gpt4 book ai didi

javascript - Jquery 为每个 ul 遍历每个 li

转载 作者:太空宇宙 更新时间:2023-11-04 15:09:21 24 4
gpt4 key购买 nike

目前有一个这样的循环,它遍历每个 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("&", "&AMP;").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("&", "&AMP;").replace(unmatchedLine.toUpperCase(), '<span class="matchedClass">' + unmatchedLine.toUpperCase() + '</span>'));
return false;
}
});
})

关于javascript - Jquery 为每个 ul 遍历每个 li,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21826530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com