开发者

jQuery .has() Target Different Element

I'm pretty new at jQuery and would like to开发者_高级运维 get better. I tried to come up with this snippet of code:

$(document).ready(function(){
    $("ul#result-list").has("div#boom")(
        function() {
        $("div.page-navigation").css("display", "none"));
    });
});

I'm trying to make div.page-navigation disappear if ul#result-list has div#boom. No luck so far despite searching all over the web. Am I far off? Thanks for the hel


.has() returns the matched dom elements, not a true or false boolean value. Instead, check the length property to determine if div#boom exists in #result-list.

Something like this:

$(document).ready(function() {
  if ($("ul#result-list").has("div#boom").length) {
    $("div.page-navigation").hide();
  }
});


Try something like this:

$(document).ready(function() {
   if($('ul#result-list:has(div#boom)').length > 0) {
      $('div.page-navigation').hide();
   }
}

This will hide the page navigation div if there's at least one result-list item that has a boom div element.


The only issues I can see, really, is that you're not chaining methods:

$(document).ready(function(){
    $("ul#result-list").has("div#boom")(
        function() {
        $("div.page-navigation").css("display", "none"));
    });
});

Which means that after .has() you're opening a parenthesis that isn't attached to a method. I'd suggest that you should try and use an if statement, rather than trying to chain, in this instance. I'd post my thoughts, but @DNR, and @Alex, got there first.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜