目前有一个像这样的循环遍历每个li,并在Li中查找某些内容并添加一个类。问题是布局有多个UL,即我希望它在每个UL中找到li。现在它停止第一个瞬间,它找到符合我的IF标准的任何li。
var list = $('.sampleAddress li span');
$(list.get().reverse()).each(function () {
var currentLine = $(this).html();
var unmatched = unmatchedLine;
if (currentLine.indexOf(unmatched) !== -1) {
$(this).html($(this).text().replace(" ", " ").replace("&", "&").replace(unmatchedLine.toUpperCase(), '<span class="matchedClass">' + unmatchedLine.toUpperCase() + '</span>'));
return false;
}
});
HTML的布局如下
<ul class="boxSlider">
<li>
<ul class="sampleAddress">
<li><span>something</span></li>
<li><span>something</span></li>
<li><span>something</span></li>
</ul>
</li>
<li>
<ul class="sampleAddress">
<li><span>something</span></li>
<li><span>something</span></li>
<li><span>something</span></li>
</ul>
</li>
</ul>
答案 0 :(得分:5)
为ul
$('.sampleAddress').each(function () {
var list = $(this).find('li span');
$(list.get().reverse()).each(function () {
var currentLine = $(this).html();
var unmatched = unmatchedLine;
if (currentLine.indexOf(unmatched) !== -1) {
$(this).html($(this).text().replace(" ", " ").replace("&", "&").replace(unmatchedLine.toUpperCase(), '<span class="matchedClass">' + unmatchedLine.toUpperCase() + '</span>'));
return false;
}
});
})