开发者

Trying to use contains selector within nested divs

开发者 https://www.devze.com 2022-12-25 18:44 出处:网络
<div> <div>test</div> </div> $(\"div:contains(\'test\')\").css(\'display\',\'none\');
<div>
    <div>test</div>
</div>

$("div:contains('test')").css('display','none');

I know I am going to kick myself on this. The problem is that when this runs all divs are hidden due to nesting. How do I make it so tha开发者_运维知识库t the parent div does not get hidden? I am limited to using 1.2.6


$("div:contains('test'):not(:has(div))").hide();


If you want an elegant solution, define a new selector. Unfortunately, :empty is not sufficient as anything with text node children isn't empty.

$.extend($.expr[':'], {
  leaf: function(elem, i, match) {
    return $(elem).children().length == 0;
  }
});

And then you can do:

$("div:leaf:contains('test')").hide();
0

精彩评论

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