简体   繁体   English

具有内联样式的元素内的元素的CSS选择器?

[英]CSS selector for element within element with inline style?

Is there a CSS selector to target elements with inline styles? 是否有一个CSS选择器来定位具有内联样式的元素? So can I target the first span but not the 2nd with CSS only? 那么我可以仅使用CSS来定位第一个跨度而不是第二个跨度吗?

If not, can this be done with jQuery? 如果没有,可以用jQuery完成吗?

http://jsfiddle.net/TYCNE/ http://jsfiddle.net/TYCNE/

<p style="text-align: center;">
    <span>target</span>
</p>

<p>
    <span>not target</span>
</p>
​

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. 如果你能完全匹配样式属性,@ simone的答案是完美的。 However, if you need to target an inline style attribute that may have other inline styles associated with it you can use: 但是,如果您需要定位可能具有与之关联的其他内联样式的内联样式属性,则可以使用:

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选择器

http://css-tricks.com/attribute-selectors/#rel-anywhere http://css-tricks.com/attribute-selectors/#rel-anywhere

p[style="text-align: center;"] {
  color: red;
}

However this is ugly. 然而这很难看。

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. 无论应用的值如何,这都将匹配具有内联样式的所有元素。

div[style*="background-image"] {
  background-size: cover;
  background-repeat: no-repeat;
}

use : 采用 :

​p[style] span {
  color: red;   
}​

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

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