jquery在里面搜索id

时间:2012-10-19 15:25:16

标签: jquery

我在jquery中开发了一个站点。我想选择包含certian字符串的所有id,例如:

<div id="home_test_div">
</div>
<span id"agency_test_span">test</span>

我希望搜索其id中包含字符串test的所有元素。 我知道如何搜索以某个字符串开头或以某个字符串结尾但在里面的id?我以前从未这样做过。 我尝试使用该代码:

var search = "test";
$("[id$='"+search+"']").each(function(index) {
  //some code
}

2 个答案:

答案 0 :(得分:4)

您可以使用attribute contains选择器:

$("[id*='"+search+"']").each(function(index) {
  //some code
})

答案 1 :(得分:2)

尝试

$("[id*='"+search+"']").each(function(index) {

您正在尝试查找ID以文字结尾的选择器。但在我的案例中,我没有看到任何以测试结尾的内容..

因此请尝试使用*替换,以搜索包含test的选择器