jQuery:选择带有自定义属性的所有元素[复制]

时间:2022-11-27 11:47:48

Possible Duplicate:
jQuery, Select by attribute value, adding new attribute
jQuery - How to select by attribute

可能重复:jQuery,按属性值选择,添加新属性jQuery——如何按属性选择

please consider this code:

请考虑这段代码:

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

how I can select All p tag with attribute MyTag?

如何选择所有带有myattribute标签的p标签?

thanks

谢谢

2 个解决方案

#1


152  

Use the "has attribute" selector:

使用“具有属性”选择器:

$('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.

还有其他“属性值以”、“属性值包含”等选项。

#2


7  

As described by the link I've given in comment, this

正如我在评论中给出的链接所描述的,这个

$('p[MyTag]').each(function(index) {
  document.write(index + ': ' + $(this).text() + "<br>");});

works (playable example).

(可演奏的例子)。

#1


152  

Use the "has attribute" selector:

使用“具有属性”选择器:

$('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.

还有其他“属性值以”、“属性值包含”等选项。

#2


7  

As described by the link I've given in comment, this

正如我在评论中给出的链接所描述的,这个

$('p[MyTag]').each(function(index) {
  document.write(index + ': ' + $(this).text() + "<br>");});

works (playable example).

(可演奏的例子)。