如何使用CSS选择器在div中选择文本(不带标签)? [英] How to select a text (without tag) in a div using CSS selector?
本文介绍了如何使用CSS选择器在div中选择文本(不带标签)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个:
<div class="foo">
<h3>Title</h3>
Some text I want to select using a CSS selector.
<div class="subfoo">Some other text</div>
</div>
是否可以使用CSS选择器的单个表达式仅选择我想使用CSS选择器检索的某些文本"?
Is it possible to select only "Some text I want to retrieve using a CSS selector" using a single expression of CSS selectors ?
推荐答案
使用纯CSS不可能做到干净.
It's not possible to do in a clean manner with pure CSS.
按照Vangel的建议,我建议在.foo
选择器中为其设置样式,然后对其他元素应用替代:
As Vangel as suggested I recommend setting the styling for it in a .foo
selector then apply overrides for other elements:
.foo{
//Some text styles
}
.foo *{
//Overriding styles
}
但是,最好的选择是对HTML进行更改,以将文本放置在其自己的元素中,以便可以单独设置样式:
However the best option would be to make changes to the HTML to place the text in its own element so it can be styled in isolation:
<div class="foo">
<h3>Title</h3>
<span>Some text</span>
<div class="subfoo">Some other text</div>
</div>
.foo span
{
//Some text styling
}
这篇关于如何使用CSS选择器在div中选择文本(不带标签)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文