简体   繁体   English

如何使用CSS类有选择地更改文本?

[英]How can I selectively change text using a CSS class?

I need to add a CSS/HTML 'fragment' to a page to change the text in the following: 我需要在页面上添加CSS / HTML“片段”以更改以下文本:

<div> 
    <h3 class="category-heading">Keep this text:</h3>
</div>
<div> 
    <h3 class="category-heading">**Change this text**:</h3>
</div>

I have tried the following: 我尝试了以下方法:

<style>
    h3.category-heading {
        text-indent: -9999px;
    }
    h3.category-heading::after {
        content: “New and Featured Products”;
        text-indent: 0;
        display: block;
        line –height: 120%;
    }
</style>

But this changed both instances of the text. 但这改变了文本的两个实例。

Is it possible to specify the second instance of the css class to be changed? 是否可以指定要更改的CSS类的第二个实例? or is it possible to select and change the wording in the second css class by adding a html fragment? 或者是否可以通过添加html片段来选择和更改第二个CSS类中的措辞?

Why not to use an id attribute? 为什么不使用id属性?

<h3 class="category-heading" id="itemThatShouldBeChanged">**Change this text**:</h3>    

and than just #itemThatShouldBeChanged {content:"other text"} 而不仅仅是#itemThatShouldBeChanged {content:“ other text”}

Supposing you can use Javascript by including it an HTML fragment : 假设您可以通过将Javascript包含HTML片段来使用它:

Depending on the inclusion mecanism (we need to know more about the tools you use), something containing : 根据包含机制(我们需要更多地了解您使用的工具),其中包含:

<script>
     window.onload = function(){
          document.getElementsByClassName("category-heading")[1].innerHTML = "New and Featured Products";
     }
</script>

If the first solution breaks some features of your website (because It could override defined behaviour), you should use : 如果第一个解决方案破坏了您网站的某些功能(因为它可能会覆盖定义的行为),则应使用:

<script>
    document.getElementsByClassName("category-heading")[1].innerHTML = "New and Featured Products";
</script>

You should take a look at nth-child Be aware that this is CSS3 您应该看一下nth-child请注意,这是CSS3

this will give you the following css selector : 这将为您提供以下css选择器:

 div:nth-child(2) h3.category-heading::after

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

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