javascript做的高亮效果

方法一:

<script>
functionHighLight(nWord){ 

if(nWord!=''){ 

varkeyword=document.body.createTextRange(); 

while(keyword.findText(nWord)){ 

keyword.pasteHTML("<spanstyle='color:red;'>"+keyword.text+"</span>"); 

keyword.moveStart('character',1); 

}

}

}

functionhighword(nWord){ 

vararray=nWord.split(","); 

for(vari=0;i<array.length;i++){ 

HighLight(array[i]);

}

}
</script>


<bodyonload="highword('li,jin')"> 

<p>liujinzhongliujinzhongliuzhong</p>

</body>

 
方法二:

<html>
<head>
<title>TestPage</title>
<scripttype="text/javascript"> 
<!--
functionhighLight(ele,keys) 
{
varreg=newRegExp("("+keys.replace(/,/,"|")+")","g"); 

ele.innerHTML=ele.innerHTML.replace(reg,"<fontcolor=\"red\">$1</font>"); 
}

window.onload=function() 
{
highLight(document.getElementsByTagName("p")[0],"li,jin"); 
}
//--> 
</script>
</head>

<body>
<p>liujinzhongliujinzhongliuzhong</p>
</body>
</html>

 

 

 

你可能感兴趣的:(JavaScript,html)