[英]How to exclude inner tag using CSS Selectors
I've the following example HTML and looking to extract the text only from the a
tag excluding the inner span
tag. 我有以下示例HTML,希望仅从
a
标记中提取文本,但不包括内部span
标记。
<a href="#" class="rate">
<span>For Sale </span>
$450.00
</a>
Is there a way I can extract the $450 only using CSS Selectors. 有没有办法我只能使用CSS选择器提取$ 450。 I tried to use
.rate
and .rate:not(span)
but none of these working 我尝试使用
.rate
和.rate:not(span)
但是这些都不起作用
**I'm looking for native CSS solutions only - Like using :not or some other. **我只在寻找本机CSS解决方案-就像使用:not或其他方法一样。 **
**
You can use Node.nextSibling
to next sibling text of element. 您可以在元素的下一个同级文本中使用
Node.nextSibling
。
var text = document.querySelector(".rate > span").nextSibling.textContent.trim(); console.log(text);
<a href="#" class="rate"> <span>For Sale </span> $450.00 </a>
Edit: If you want to use CSS, you can store price value in data-*
attribute of .rate
and use :after
pseudo-elements in it. 编辑:如果你想使用CSS,你可以价值存储在
data-*
的属性.rate
和使用:after
在其伪元素。 You can use attribute value of parent in content
property of pseudo-elements using CSS attr
. 您可以使用CSS
attr
在伪元素的content
属性中使用parent的属性值。
.rate:after { content: attr(data-price); color: red; }
<a href="#" class="rate" data-price="$450.00"> <span>For Sale </span> </a>
Using css you can only hide the element so that its text gets hide. 使用css,您只能隐藏元素,以便隐藏其文本。
for ex: 例如:
a.rate span {
display : none;
}
If you wan to achieve to get text only $450, You should go with Javascript or jquery. 如果您想获得仅$ 450的文本,则应使用Javascript或jquery。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.