javascript高亮効果の二種類の実現方法

1698 ワード

jsハイライト方法一:
 
  
<br>function HighLight(nWord){ <br> <br>if(nWord!=''){ <br> <br>var keyword = document.body.createTextRange(); <br> <br>while(keyword.findText(nWord)){ <br> <br>keyword.pasteHTML("<span style='color:red;'>" + keyword.text + "</span>"); <br> <br>keyword.moveStart('character',1); <br> <br>} <br> <br>} <br> <br>} <br> <br>function highword(nWord){ <br> <br>var array = nWord.split(","); <br> <br>for(var i=0;i<array.length;i++){ <br> <br>HighLight(array[i]); <br> <br>} <br> <br>} <br>




liujinzhong liujinzhong liuzhong




jsハイライト方法2:
2.
 
  


Test Page
<br><!-- <br>function highLight(ele,keys) <br>{ <br>var reg = new RegExp("(" + keys.replace(/,/,"|") + ")","g"); <br> <br>ele.innerHTML = ele.innerHTML.replace(reg,"<font color=\"red\">$1</font>"); <br>} <br> <br>window.onload = function() <br>{ <br>highLight(document.getElementsByTagName("p")[0],"li,jin"); <br>} <br>//--> <br>



liujinzhong liujinzhong liuzhong