开发者

Trying to use contains selector within nested divs

<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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜