我想在 DOM 上做一些操作,ID 像'abc%'
<a id='abc1'></a>
<a id='abc2'></a>
<a id='abc3'></a>
<a id='abc4'></a>
<a id='1234'></a>
在上面的代码中,我必须对所有具有 id 像"abc%"的锚点执行操作
如何使用jquery?
你可以使用这个:
$('a[id^="abc"]')
它被称为属性以选择器开头。
在这里你可以看到它的工作:http://jsfiddle.net/suLsx/
正如 Matti 正确指出的那样,向这些锚标签添加一个额外的类会更干净,以便创建一个更容易使用的选择器:
<a class="the_link" id='abc1'></a>
<a class="the_link" id='abc2'></a>
<a class="the_link" id='abc3'></a>
<a class="the_link" id='abc4'></a>
<a id='1234'></a>
现在,您将能够执行以下操作:
$( "a.the_link" ); // this selector will now operate on all the desired elements
$( "a.the_link" ).hide(); // hide all the links
$( "a.the_link" ).fadeOut(); // fade out all the links
像这样尝试,
alert($( "a[id^='abc']" ).length);
读取属性从选择器开始
工作小提琴