Home  >  Article  >  Web Front-end  >  JavaScript web page keyword highlighting code_javascript skills

JavaScript web page keyword highlighting code_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:02:14874browse

//Javascript code that highlights web page keywords

var keys = [
                                                                                                      
                                                                                                          
                                                                                                                                                                          
                                                                                                                                                                                
                                   
var bookmark;
if(document.createRange){
var range = document.createRange();
}else{
var range = document.body.createTextRange();
bookmark = range.getBookmark();
}
var key;
for(var i = 0;key = keys[i];i ){
if(range.findText){
Range.collapse(true);
range.moveToBookmark(bookmark);
while(range.findText(key[0])){
Range.pasteHTML(range.text.fontcolor(key[2]));
       }
}else{
var s,n;
s = window.getSelection();
s.collapse(document.body,0);
while(window.find(key[0])){
var n = document.createElement("SPAN");
n.style.color=key[2];
s.getRangeAt(0).surroundContents(n);
       }
}
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn