환//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
개선된 버전
코드 복사
코드는 다음과 같습니다.
function 하이라이트Word(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, node);
pn.insertBefore(이후, 노드);
pn.removeChild(노드);
}
}
}
}
//根据Tag명 高亮关键字
function SearchHighlightTag(node, key) {
if (!document.createElement) return ;
if (key.length === 0) false를 반환합니다.
var array = new Array();
배열 = key.split(" ");
var 요소 = document.getElementsByTagName(노드);
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 array = new Array();
배열 = key.split(" ");
var 요소 = document.getElementById(node);
for (var i = 0; i for (var j = 0; j highlightWord(요소, array[i ]);
}
}
}