对dom的行为有id LIKE'abc%'

Vin*_*abu 2 javascript php jquery dom

我想对DOM做一些动作就像'abc%'这样的ID

<a id='abc1'></a>
<a id='abc2'></a>
<a id='abc3'></a>
<a id='abc4'></a>
<a id='1234'></a>
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我必须对所有那些像'abc%'这样的id进行操作

如何使用jquery?

Joe*_*Joe 7

你可以用这个:

$('a[id^="abc"]')
Run Code Online (Sandbox Code Playgroud)

它被称为属性开始选择器.

在这里你可以看到它工作:http://jsfiddle.net/suLsx/


Lix*_*Lix 5

正如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>
Run Code Online (Sandbox Code Playgroud)

现在你将能够做到这一点:

$( "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
Run Code Online (Sandbox Code Playgroud)