javascript高亮效果的二种实现方法
时间:2020-11-25 11:56:18|栏目:JavaScript代码|点击: 次
js高亮方法一:
<script>
function HighLight(nWord){
if(nWord!=''){
var keyword = document.body.createTextRange();
while(keyword.findText(nWord)){
keyword.pasteHTML("<span style='color:red;'>" + keyword.text + "</span>");
keyword.moveStart('character',1);
}
}
}
function highword(nWord){
var array = nWord.split(",");
for(var i=0;i<array.length;i++){
HighLight(array[i]);
}
}
</script>
<body onload="highword('li,jin')">
<p>liujinzhong liujinzhong liuzhong</p>
</body>
js高亮方法二:
2.
<html>
<head>
<title>Test Page</title>
<script type="text/javascript">
<!--
function highLight(ele,keys)
{
var reg = new RegExp("(" + keys.replace(/,/,"|") + ")","g");
ele.innerHTML = ele.innerHTML.replace(reg,"<font color=\"red\">$1</font>");
}
window.onload = function()
{
highLight(document.getElementsByTagName("p")[0],"li,jin");
}
//-->
</script>
</head>
<body>
<p>liujinzhong liujinzhong liuzhong</p>
</body>
</html>
复制代码 代码如下:
<script>
function HighLight(nWord){
if(nWord!=''){
var keyword = document.body.createTextRange();
while(keyword.findText(nWord)){
keyword.pasteHTML("<span style='color:red;'>" + keyword.text + "</span>");
keyword.moveStart('character',1);
}
}
}
function highword(nWord){
var array = nWord.split(",");
for(var i=0;i<array.length;i++){
HighLight(array[i]);
}
}
</script>
<body onload="highword('li,jin')">
<p>liujinzhong liujinzhong liuzhong</p>
</body>
js高亮方法二:
2.
复制代码 代码如下:
<html>
<head>
<title>Test Page</title>
<script type="text/javascript">
<!--
function highLight(ele,keys)
{
var reg = new RegExp("(" + keys.replace(/,/,"|") + ")","g");
ele.innerHTML = ele.innerHTML.replace(reg,"<font color=\"red\">$1</font>");
}
window.onload = function()
{
highLight(document.getElementsByTagName("p")[0],"li,jin");
}
//-->
</script>
</head>
<body>
<p>liujinzhong liujinzhong liuzhong</p>
</body>
</html>
上一篇:JavaScript验证知识整理
栏 目:JavaScript代码
下一篇:微信小程序实现时间预约功能
本文地址:http://www.codeinn.net/misctech/26070.html