改进版
functionhighlightWord(node, word) {
// 迭代到此节点 childNodes
if (node.hasChildNodes) {
var hi_cn;
for (hi_cn = 0; hi_cn highlightWord(node.childNodes[hi_cn], word);
}
}
// 执行此节点本身
if (node.nodeType == 3) { // 文本节点
tempNodeVal = node.nodeValue.toLowerCase();
tempWordVal = word.toLowerCase();
if (tempNodeVal.indexOf(tempWordVal) != -1) {
pn = node.parentNode;
if (pn.className != "highlight") {
// 单词尚未突出显示!
nv = node.nodeValue;
ni = tempNodeVal.indexOf(tempWordVal);
// 创建替换节点负载
before = document.createTextNode(nv.substr(0, ni));
docWordVal = nv.substr(ni, word.length);
after = document.createTextNode(nv.substr(ni word.length));
hiwordtext = document.createTextNode(docWordVal);
hiword = document.createElement("span");
hiword.className = "突出显示";
hiword.appendChild(hiwordtext);
pn.insertBefore(之前,节点);
pn.insertBefore(hiword, 节点);
pn.insertBefore(之后,节点);
pn.removeChild(节点);
}
}
}
}
//根据Tag名高亮关键字
function SearchHighlightTag(node, key) {
if (!document.createElement) return ;
if (key.length === 0) 返回 false;
var 数组 = new Array();
array = key.split(" ");
var element = document.getElementsByTagName(node);
for (var i = 0; i for (var j = 0; j highlightWord(element[j],数组[i]);
}
}
}
//根据ID高亮关键字
function SearchHighlightID(node, key) {
if (!document.createElement) return;
if (key.length === 0) 返回 false;
var 数组 = new Array();
array = key.split(" ");
var element = document.getElementById(node);
for (var i = 0; i for (var j = 0; j highlightWord(element, array[i ]);
}
}
}