目前有一个像这样的循环,它遍历每个 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>
最佳答案
对 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;
}
});
})
关于javascript - Jquery 循环遍历每个 li 的每个 ul,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21826530/