如何使用CSS或JQuery为文本上色 [英] How to color portion of text using css or jquery
本文介绍了如何使用CSS或JQuery为文本上色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一段文字,在一个特殊字符(如%word%)内几乎没有单词.我只想将段落中的这些单词涂成红色.我可以在纯CSS中执行此操作吗?如果不是,通过jQuery为它着色的正则表达式应该是什么. 示例:
<Html>
<head>
<style>
</style>
</head>
<body>
<div id="myelement">
Hi how do I color %this%?
</div>
</body>
</Html>
解决方案
我在javascript
中使用了正则表达式(regex),如下所示:
function chnColor(){
var str=document.getElementById("myelement").innerHTML;
str=str.replace(/(%)(.*)(%)/g,"<font color='red'>$2</font>");
//Here $2 Means Anything in between % and % This is what you need to color
document.getElementById("myelement").innerHTML=str;
}
>
DEMO
希望有帮助!欢呼:)!
I have a paragraph of text which has few words inside a special character like %word%. I want to color only these words of paragraph in red. Can I do this in pure css? If not what should be the regex for coloring it through jquery. Example:
<Html>
<head>
<style>
</style>
</head>
<body>
<div id="myelement">
Hi how do I color %this%?
</div>
</body>
</Html>
解决方案
I've used regular expression(regex) in javascript
as follows:
function chnColor(){
var str=document.getElementById("myelement").innerHTML;
str=str.replace(/(%)(.*)(%)/g,"<font color='red'>$2</font>");
//Here $2 Means Anything in between % and % This is what you need to color
document.getElementById("myelement").innerHTML=str;
}
Hope it helps! cheers :)!
这篇关于如何使用CSS或JQuery为文本上色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文