Child Div过滤不起作用

Filtering by Child Div not working

本文关键字:不起作用 过滤 Div Child      更新时间:2023-09-26

我试图通过查看子元素的id来过滤div元素,但我似乎无法使其正常工作,也无法找出原因。

html:

<div class="section-link" id="section-tooltip">
    <div class="brand tp" style="display: none;"></div>
    contents
</div>
<div class="section-link" id="section-tooltip">
    <div class="brand garden" style="display: none;"></div>
    contents
</div>

js:

function brand(string){
    var brand = string;
    $('.section-link').hide();
    if ($(".section-link").children('.brand').hasClass(brand)) {
        $(this).parent().show();
    }
}

然后我通过chrome浏览器执行以下操作:javascript:brand("tp");

它隐藏了所有的div,但是它没有显示中带有tp元素的div

$("this")是错误的。

$(this) //this is right

编辑。另一个:它不是

.hasclass()

但是

.hasClass()

这段代码:

if ($(".section-link").children('.brand').hasClass(brand)) {
   $("this").parent().show();
}

应该改为:

$(".section-link").children('.brand').each(function(){
   if($(this).find(brand).length > 0){
      $(this).find(brand)[0].parent().show(); //assuming onlt the first 'tp's parent needs to be shown
   }
});

PS:这不需要引号