高亮网页中的关键字

function highlight(word){
    var range;
    if(document.createRange){
        range = document.createRange();
    }else{
        range = document.body.createTextRange();
    }
    if(range.findText){
        range.collapse(true);
        range.moveToBookmark(range.getBookmark());
        while(range.findText(word)){
            range.pasteHTML(range.text.fontcolor("red"));
        }
    }else{
        var s,n;
        s = window.getSelection();
        s.collapse(document.body,0);
        while(window.find(word)){
            n = document.createElement("SPAN");
            n.style.color = "red";
            s.getRangeAt(0).surroundContents(n);
        }
    }
}

你可能感兴趣的:(关键字)