简体   繁体   English

使用jQuery在单独的无序列表中查找具有匹配类名的列表项

[英]Use jQuery to find list items with matching class names in separate unordered lists

I have two unordered lists, each filled with list items that have a DYNAMIC class name. 我有两个无序列表,每个列表都填充了具有DYNAMIC类名的列表项。 When I say "dynamic" I mean they are not generated by me, but they don't change once the lists have been created. 当我说“动态”时,我的意思是它们不是由我生成的,但是一旦创建了列表,它们就不会改变。 These class names are id's I'm getting from an API, so they're just random numbers. 这些类名是我从API获取的id,因此它们只是随机数。 A simple example would be something like... 一个简单的例子就是......

<ul class="listA">
  <li class="123"></li>
  <li class="456"></li>
  <li class="789"></li>
</ul>

<ul class="listB">
  <li class="789"></li>
  <li class="101"></li>
  <li class="112"></li>
</ul>

What I'm trying to do is compare the two lists, and have any matches be highlighted, in this case the items with the class "789" would match. 我要做的是比较两个列表,并突出显示任何匹配项,在这种情况下,类“789”的项目将匹配。 When I say highlighted, I just mean I'll probably apply some css after a match is found, like maybe a background color or something (not too important yet). 当我说突出显示时,我只是意味着我可能会在找到匹配后应用一些css,例如背景颜色或其他东西(不太重要)。 The problem really lies in the fact that the lists can be somewhat long (maybe 50 items) and the classes are just random numbers I don't choose, so I can't do any specific searches. 问题实际上在于列表可能有点长(可能是50个项目)而且类只是我不选择的随机数,因此我无法进行任何特定搜索。 Also, there will most likely be cases with multiple matches, or no matches at all. 此外,很可能会有多个匹配的情况,或根本没有匹配。

I'm pretty new to jQuery, so there may be a fairly simple answer, but everything I find online refers to searching by a specific class, such as the .find() method. 我对jQuery很新,所以可能有一个相当简单的答案,但我在网上找到的所有内容都是指通过特定的类进行搜索,例如.find()方法。 If anyone needs more info or a better example, I'll be happy to give more info, I'm just trying to keep it simple now. 如果有人需要更多信息或更好的例子,我会很乐意提供更多信息,我现在只是想保持简单。

Thanks so much in advance! 非常感谢提前!

var $first  = $('ul.listA li'),
    $second = $('ul.listB li');

$first.each(function(){
    var cls = this.className,
        $m  = $second.filter(function(){
            return this.className === cls;
        });

    if ($m.length) {
      $(this).add($m).addClass('matched');
    }
});

http://jsfiddle.net/b4vFn/ http://jsfiddle.net/b4vFn/

Try it this way: 试试这种方式:

    $("ul.listA li").each(function(){
         var listAval = $(this).attr('class');
         $("ul.listB li").each(function(){
            if(listAval == $(this).attr('class')){
              //matched..
              return false; //exit loop..
            }
         }
    }

A slightly less verbose variant of Nix's answer: 一个稍微不那么冗长的Nix答案的变体:

$("ul.listA li").each(function(){
    var a = $("ul.listB li").filter("." + $(this).attr('class'));
    if (a.size()) {
        a.add($(this)).css("background", "red");
    }
});

you can find the code here: jsFiddle 你可以在这里找到代码: jsFiddle

var listA=$('.listA li')
var listB=$('.listB li')

listA.each(function(){
    var classInA=$(this).attr('class');
    listB.each(function(){
        var classInB=$(this).attr('class');
        if(classInA === classInB){
             console.log(classInA);
             //now you found the same one
        }
    })
})

Demo at http://jsfiddle.net/habo/kupd3/ 演示http://jsfiddle.net/habo/kupd3/

   highlightDups();

   function highlightDups(){
    var classes = [] ;
    $('ul[class^="list"]').each(function(k,v){
        //alert(v.innerHTML);
        $($(this).children()).each(function(nK,nV){
           // alert($(this).attr("class"));
            classes.push($(this).attr("class"));
        });
    });
        hasDuplicate(classes);
    }

//Find duplicate picked from fastest way to detect if duplicate entry exists in javascript array? //查找从最快方式挑选的重复以检测javascript数组中是否存在重复条目?

function hasDuplicate(arr) {
    var i = arr.length, j, val;
    while (i--) {
        val = arr[i];
        j = i;
        while (j--) {
            if (arr[j] === val) {
 // you can write your code here to handle when you find a match
                $("."+val).text("This is Duplicate").addClass("match");
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM