方法一:
- <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>
- <scripttypescripttype="text/javascript">
- <!--
- functionhighLight(ele,keys)
- {
- varreg=newRegExp("("+keys.replace(/,/,"|")+")","g");
- eleele.innerHTML=ele.innerHTML.replace(reg,"<fontcolorfontcolor=\"red\">$1</font>");
- }
- window.onload=function()
- {
- highLight(document.getElementsByTagName("p")[0],"li,jin");
- }
- //-->
- </script>
- </head>
- <body>
- <p>liujinzhongliujinzhongliuzhong</p>
- </body>
- </html>