This question already has answers here:
How to get elements which have no children, but may have text?

(3 个回答)


7年前关闭。




说我的标记是:
<ul>
    <li id="num1">hello</li>
    <li id="num2"><p>hello</p></li>
    <li id="num3">sdf dfg</li>
    <li id="num4"></li>
    <li id="num5"><a>linky</a></li>
</ul>

我如何查询 ul 以返回 #num1, #num3 and #num4(没有子标签的那些)?

就像是:
$('ul').children('li:empty')

但是来自文档:
:empty  Returns: Array<Element(s)>
Matches all elements that have no children (including text nodes).

因为它包含文本,上面的查询只返回我 #num4

我应该/我可以做什么?

编辑:我也在使用 jQuery 1.1.4(不要问)

谢谢

最佳答案

How to get elements which have no children, but may have text?

10-08 15:13