javascript做的高亮效果

1.
<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>
 
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,html)