How about
$("span:not(:has(*))")
Selects all spans that have no children.
Explanation
The :has()
selector "selects elements which contain at least one element that matches the specified selector." The wildcard *
means all elements.
The expression $('div:has(p)')
matches a <div>
if a <p>
exists
anywhere among its descendants, not just as a direct child.
The :not()
selector "selects all elements that do not match the given selector."
In this case, :has()
selects everything and then we use :not()
to find the elements that don't match "everything"... in other words, nothing.
Demo
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…