开发者

jQuery selector UL that contains NO LIs

Assuming the following HTML how can I find all uls that do NOT contain any lis? IE: In this case ul3 and ul4?

I can see that jQuery has the "has" selector but I can't see the inverse of it? Nor do I understand how I could combine has and not to accomplish what I 开发者_高级运维need.

<div id="div1">
    <ul id='ul1'>
        <li>
        </li>
    </ul>

    <ul id='ul2'>
        <li>
        </li>
    </ul>

    <ul id='ul3'>
    </ul>

    <ul id='ul4'>
    </ul>
</div>


Nor do I understand how I could combine has and not to accomplish what I need.

As far as I can tell, it's as simple as:

$('ul:not(:has(li))')


Use the :empty selector (if you are sure there are no text node descendent too).

$('ul:empty');

Otherwise you could use...

$('ul').filter(function() { return ! $(this).find('li').length; });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜