複製程式碼 程式碼如下:
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 ]);
}
}
}