使用jQuery确定内容是否唯一,然后选择样式

使用jQuery确定内容是否唯一,然后选择样式,jquery,filter,unique,Jquery,Filter,Unique,在一个充满div的页面上,我需要确定div的内容是否是唯一的,如果是,则对其应用样式,可能是隐藏 我的问题是,我需要检查具有相同类的div列表,并对照具有相同类的div列表检查文本内容 例如,如果我有一个具有picker类的div,如果它的文本内容不在具有类fruits的div列表中,我需要隐藏该div 我尝试了jqueryunique和filter,但无法接近 我在fiddle上设置了一个例子来解释这个问题,基于这个fiddle,如何基于没有其他div包含cherry的事实来隐藏包含cherr

在一个充满div的页面上,我需要确定div的内容是否是唯一的,如果是,则对其应用样式,可能是隐藏

我的问题是,我需要检查具有相同类的div列表,并对照具有相同类的div列表检查文本内容

例如,如果我有一个具有picker类的div,如果它的文本内容不在具有类fruits的div列表中,我需要隐藏该div

我尝试了jqueryunique和filter,但无法接近

我在fiddle上设置了一个例子来解释这个问题,基于这个fiddle,如何基于没有其他div包含cherry的事实来隐藏包含cherry的div


您可以使用.html函数获取div的内容,如下所示:

$(".left div").each(function(){
    var bool = true;
    var selected = $(this);
    $(".left div").not(selected).each(function(){
        if(selected.html() == $(this).html()){
            bool = false;
            return false;
        }
    });
    if(bool == false){
        console.log(selected.html() + "not unique");
    }else{
        console.log(selected.html() + "unique");
    }
});
$(".right div").each(function(){
    var bool = true;
    var selected = $(this);
    $(".right div").not(selected).each(function(){
        if(selected.html() == $(this).html()){
            bool = false;
            return false;
        }
    });
    if(bool == false){
        console.log(selected.html() + "not unique");
    }else{
        console.log(selected.html() + "unique");
    }
});
看看这个

$(".left div").each(function(){
    var bool = true;
    var selected = $(this);
    $(".left div").not(selected).each(function(){
        if(selected.html() == $(this).html()){
            bool = false;
            return false;
        }
    });
    if(bool == false){
        console.log(selected.html() + "not unique");
    }else{
        console.log(selected.html() + "unique");
    }
});
$(".right div").each(function(){
    var bool = true;
    var selected = $(this);
    $(".right div").not(selected).each(function(){
        if(selected.html() == $(this).html()){
            bool = false;
            return false;
        }
    });
    if(bool == false){
        console.log(selected.html() + "not unique");
    }else{
        console.log(selected.html() + "unique");
    }
});