简体   繁体   English

仅选择没有子元素的元素,除了子元素是</wbr>

[英]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. 在这种情况下,即使其中包含子元素,我也要选择p。 But just if the child element is a wbr tag. 但是,即使子元素是wbr标签。

You can have another :not() within the :has() for excluding certain child elements: 你可以有另一种:not() :has()用于排除某些子元素:

$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: 在一个侧面说明,如果外部:not()是你的唯一部分.filter()选择字符串,你可以简单地交换.filter()出去.not()以使代码少一点困惑:

$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 . 这两个语句在英语中的含义相同:从集合$mainSection中排除具有非wbr子元素的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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