[英]Select only elements that have no children except the child element is a <wbr></wbr>
我想选择没有子元素的元素。 这是因为我想在一个非常复杂的网站中选择所有文本。
我这样做是这样的:
$mainSection.filter(":not(:has(*))");
但是,还有一些特殊情况,例如:
<p>Some interesting text <wbr></wbr> that has an "optional word break" tag in it</p>
在这种情况下,即使其中包含子元素,我也要选择p。 但是,即使子元素是wbr标签。
你可以有另一种:not()
内 :has()
用于排除某些子元素:
$mainSection.filter(":not(:has(:not(wbr)))");
在一个侧面说明,如果外部:not()
是你的唯一部分.filter()
选择字符串,你可以简单地交换.filter()
出去.not()
以使代码少一点困惑:
$mainSection.not(":has(:not(wbr))");
这两个语句在英语中的含义相同:从集合$mainSection
中排除具有非wbr
子元素的元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.