简体   繁体   中英

Select only elements that have no children except the child element is a <wbr></wbr>

I want to select elements that have no child elements. This is because I want to select all the texts in a very complex website.

I do this like so:

$mainSection.filter(":not(:has(*))");

But then there are some special cases like this one:

<p>Some interesting text <wbr></wbr> that has an "optional word break" tag in it</p>

In this case I want to select the p, even if there is child element in it. But just if the child element is a wbr tag.

You can have another :not() within the :has() for excluding certain child elements:

$mainSection.filter(":not(:has(:not(wbr)))");

On a side note, if the outer :not() is the only part of your .filter() selector string, you can simply swap the .filter() out for a .not() to make the code a little less confusing:

$mainSection.not(":has(:not(wbr))");

Both of these statements mean the same thing in English: exclude elements from the set $mainSection that have any child elements that are not wbr .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM