Html 具有内联样式的元素内元素的 CSS 选择器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14139690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS selector for element within element with inline style?
提问by Evanss
Is there a CSS selector to target elements with inline styles? So can I target the first span but not the 2nd with CSS only?
是否有一个 CSS 选择器来定位具有内联样式的元素?那么我可以仅使用 CSS 定位第一个跨度而不是第二个跨度吗?
If not, can this be done with jQuery?
如果没有,这可以用jQuery完成吗?
<p style="text-align: center;">
<span>target</span>
</p>
<p>
<span>not target</span>
</p>
?
采纳答案by Simone
p[style="text-align: center;"] {
color: red;
}
However this is ugly.
然而这是丑陋的。
回答by Dan
A bit late to the tea party but thought I would share the solution I found & use.
茶话会有点晚了,但我想我会分享我发现和使用的解决方案。
@simone's answer is perfect if you can match the style attribute exactly. However, if you need to target an inline style attribute that may have other inline styles associated with it you can use:
如果您可以完全匹配 style 属性,@simone 的答案是完美的。但是,如果您需要定位可能具有与其关联的其他内联样式的内联样式属性,您可以使用:
p[style*="text-align:center;"]
"*=" means "match the following value anywhere in the attribute value."
“*=”表示“与属性值中的任何位置匹配以下值”。
For further reference or more detailed information on other selectors see this blog post on css-tricks.com:
有关其他选择器的进一步参考或更多详细信息,请参阅 css-tricks.com 上的这篇博文:
The Skinny On CSS Selectors
CSS 选择器上的瘦身
回答by Nathan Shanahan
If you would like to apply styles to a particular rule declaration you can also use style*. This will match all elements that have the inline style, regardless of the value applied.
如果您想将样式应用于特定规则声明,您还可以使用 style*。这将匹配所有具有内联样式的元素,无论应用的值如何。
div[style*="background-image"] {
background-size: cover;
background-repeat: no-repeat;
}
回答by Jerome Cance
use :
用 :
?p[style] span {
color: red;
}?