我想保留一个具有相同类名的所有div的列表,即mod。为此,我根据升序ID对列表进行了排序。因此,我正在寻找那些内部div包含带有文本“Hello1”的类标题的ID。
这是结构
<div class="mod" id="mod23" >
<div class="content" >
<div class="hd" >
<div class="title">Hello1</div>
<ul class="list"></ul>
<ul class="buttons">
<li class="help"></li>
<li class="show" ></li>
<li class="close"></li>
</ul>
</div>
</div>
</div>
<div class="mod" id="mod27" >
<div class="content" >
<div class="hd" >
<div class="title">Hello2</div>
<ul class="list"></ul>
<ul class="buttons">
<li class="help"></li>
<li class="show" ></li>
<li class="close"></li>
</ul>
</div>
</div>
</div>
<div class="mod" id="mod25" >
<div class="content" >
<div class="hd" >
<div class="title">Hello1</div>
<ul class="list"></ul>
<ul class="buttons">
<li class="help"></li>
<li class="show" ></li>
<li class="close"></li>
</ul>
</div>
</div>
</div>
<div class="mod" id="mod29" >
<div class="content" >
<div class="hd" >
<div class="title">Hello2</div>
<ul class="list"></ul>
<ul class="buttons">
<li class="help"></li>
<li class="show" ></li>
<li class="close"></li>
</ul>
</div>
</div>
</div>
以下是我试图提出的代码
myarray is a sorted list of unique id number.
for(var i=0;i<len;i++)
{
$('#mod'+myarray[i]).each(function(index) {
$('div.title:contains(Hello1)').each(function(index) {
alert('found at ' +i);
});
});
}
答案 0 :(得分:1)
而不是for
循环和:
$('#mod'+myarray[i]).each
您可以使用starts with
选择器,如下所示:
$('div[id^="mod"]').each(function(){
// this iterates over all divs with id starting like "modNUMBER"
});
但是我认为这就是你要找的东西:
$('div.title:contains("Hello1")').each(function(){
var id = $(this).closest('.mod')[0].id;
alert('Found in div with id: ' + id);
});