简体   繁体   English

更改特定元素的CSS内容属性

[英]Change CSS content attribute of a specific element

Given the style : 给定样式:

ol.progtrckr li.progtrckr-todo:before {
    content: "\039F";
    color: silver;
    background-color: white;
    font-size: 1.5em;
    bottom: -1.6em;
}

and the objects : 和对象:

<ol class="progtrckr" data-progtrckr-steps="5">
    <li class="progtrckr-todo">One</li>
    <li class="progtrckr-todo">Two</li>
    <li class="progtrckr-todo">Three</li>
</ol>

How can I change the content property of the second object only , from 我如何才能仅更改第二个对象的content属性?

content: "\039F"; 

to

content: "\\2716"; ?

I tried : 我试过了 :

<li class="progtrckr-todo" style="content: '\2716'">Two</li>

But it doesn't work . 但这没用。

Any idea how to make this work ? 任何想法如何使这项工作?

content property is available only on pseudo elements ( :before , :after ). content属性仅在伪元素( :before:after )上可用。 You cannot overwrite it with an inline style since an inline style always applies to the element it's on, and pseudo elements are not part of the markup you can edit. 您不能用内联样式覆盖它,因为内联样式始终适用于其所在的元素,并且伪元素不是您可以编辑的标记的一部分。

Use either :nth-of-type or :nth-child pseudo selector: 使用:nth-of-type:nth-child伪选择器:

ol.progtrckr :nth-child(2):before {
    content: "\2716";
}

You can use nth-child to override the properties 您可以使用nth-child覆盖属性

ol.progtrckr :nth-child(2):before {
    content: "\2716";
}

Should be something like this: 应该是这样的:

ol.progtrckr :nth-child(2):before {
 content: "\2716";
}

you can refeer to this if its not exactly what you need 如果不是您所需要的,您可以参考

http://www.w3schools.com/cssref/sel_nth-child.asp http://www.w3schools.com/cssref/sel_nth-child.asp

The content:'' property is a css property of the pseudo element :before and not the li element. content:''属性是伪元素:before而不是li元素的css属性。

If you want to change the content programatically, you'd better just add some class to the li element, and override the li.yourClass:before{content:'...'} in your css. 如果要以编程方式更改content ,则最好只向li元素添加一些类,然后在li.yourClass:before{content:'...'}覆盖li.yourClass:before{content:'...'}

You problem is that in you css styles it works with ":before". 您的问题是,在css样式中,它与“:before”一起使用。 So you need to insert content before element. 因此,您需要在element之前插入内容。 You can use nth-child W3C 您可以使用nth-child W3C

Or just add specific class to sec element: 或者只是将特定的类添加到sec元素中:

  secelementclass:before {
        content: "\2716";
    }

<ol class="progtrckr" data-progtrckr-steps="5">
<li class="progtrckr-todo">One</li>
<li class="progtrckr-todo secelementclass">Two</li>
<li class="progtrckr-todo">Three</li>

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

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