简体   繁体   English

CSS / JavaScript - 文本中的样式关键字?

[英]CSS / JavaScript - Styling keywords in text?

There is some text, and need to change color of some keywords, without using code in text.有一些文字,需要改变一些关键字的颜色,不需要在文字中使用代码。 How can I do that with CSS / JS ?我怎样才能用 CSS/JS 做到这一点?

<p>Lorem ipsum RED dolor sit amet, RED consectetur adipiscing elit, RED sed do eiusmod 
   tempor incididunt ut labore et dolore RED magna aliqua.</p>

One way to achieve this is to replace the text "RED" , with a span that has a style to color it: "<span style='color:red'>"实现此目的的一种方法是将文本"RED"替换为具有为其着色的样式的跨度: "<span style='color:red'>"

 var e = document.getElementById("text"); var txt = e.innerHTML; e.innerHTML = txt.replace(new RegExp("RED", "g"),"<span style='color:red'>RED</span>");
 <p id="text">Lorem ipsum RED dolor sit amet, RED consectetur adipiscing elit, RED sed do eiusmod tempor incididunt ut labore et dolore RED magna aliqua.</p>

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

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