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 } }