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();
精彩评论