jQuery:选择具有自定义属性的所有元素

jQuery:选择具有自定义属性的所有元素

问题描述:


可能重复:

jQuery,按属性值选择,添加新属性

jQuery - 如何按属性选择

请考虑以下代码:

<p>11111111111111</p>
<p MyTag="nima">2222222222</p>
<p>33333333333</p>
<p MyTag="Sara">>4444444444</p>

我如何选择所有 p 标签属性 MyTag

how I can select All p tag with attribute MyTag?

谢谢

使用具有属性选择器

$('p[MyTag]')

或者选择该属性具有特定值的一个:

Or to select one where that attribute has a specific value:

$('p[MyTag="Sara"]')

其他选择器,属性值以开头,属性值包含等等。

There are other selectors for "attribute value starts with", "attribute value contains", etc.