html关键字高亮

/**
* @id 父节点id
* @key 关键字
*/
function keyLight(id, key, bgColor){
    var oDiv = document.getElementById(id),
            oChilds  = oDiv.childNodes,
            bgColor = bgColor || "orange",
            sKey = "<b style='">";'>"+key+"</b>",
            rStr = new RegExp(key, "g");
    for(var i =0; i<oChilds.length-1; i++){
        if(oChilds[i].nodeType == 3 && /\s/.test(oChilds[i].nodeValue)){  //删除空白的节点
            oChilds[i].parentNode.removeChild(oChilds[i]);
        }
        oChilds[i].innerHTML = oChilds[i].innerHTML.replace(rStr,sKey);  //替换key
    }
}

 

posted @ 2020-09-20 20:59  一步至遥  阅读(1669)  评论(0编辑  收藏  举报
TOP