Jquery遍历每个ul的每个li

时间:2014-02-17 10:29:16

标签: javascript jquery html

目前有一个像这样的循环遍历每个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>

1 个答案:

答案 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("&", "&AMP;").replace(unmatchedLine.toUpperCase(), '<span class="matchedClass">' + unmatchedLine.toUpperCase() + '</span>'));
            return false;
        }
    });
})