首頁 > web前端 > js教程 > js 關鍵字高亮(根據ID/tag高亮關鍵字)案例介紹_javascript技巧

js 關鍵字高亮(根據ID/tag高亮關鍵字)案例介紹_javascript技巧

WBOY
發布: 2016-05-16 17:43:09
原創
1412 人瀏覽過
複製程式碼 程式碼如下:





JS 關鍵字高亮 title> <br><script type="text/javascript"> <BR>/* <BR>* 參數說明: <BR>* obj: 物件, 要進行高亮顯示的html標籤節點. <BR>* hlWords : 字串, 要進行高亮的關鍵字詞, 使用豎槓(|)或空格分隔多個字. <BR>* bgColor: 背景顏色,預設為紅色. <BR>*/ <BR>function MarkHighLight( obj, hlWords, bgColor) { <BR>hlWords = AnalyzeHighLightWords(hlWords); <BR>if (obj == null || hlWords.length == 0) <BR>return; <bullg if (bullg== | bgColor == "") { <BR>bgColor = "red"; <BR>} <BR>MarkHighLightCore(obj, hlWords); <BR>//執行高亮標記的核心方法<BR>function MarkHighLightCore(objghLightCore(objghLightCore(objghLightCore(objghLightCore(objghLightCore)。 , keyWords) { <BR>var re = new RegExp(keyWords, "i"); <BR>var style = ' style="background-color:' bgColor ';" ' <BR>for (var i = 0; i < obj.childNodes.length; i ) { <BR>var childObj = obj.childNodes[i]; <BR>if (childObj.nodeType == 3) { <BR>if (childObj.data.search(re) == -1) continue; <BR>var reResult = new RegExp("(" keyWords ")", "gi"); <BR>var objResult = document.createElement("span"); <BR>objResult = document.createElement("span"); <BR>objResult.innerobjResult.innerHTMLResult. = childObj.data.replace(reResult, "<span" style ">$1"); <BR>if (childObj.data == objResult.childNodes[0].innerHTML) continue; <BR>obj. replaceChild(objResult, childObj); <BR>} else if (childObj.nodeType == 1) { <BR>MarkHighLightCore(childObj, keyWords); <BR>} <BR>} <關鍵字<BR>function AnalyzeHighLightWords(hlWords) { <BR>if (hlWords == null) return ""; <BR>hlWords = hlWords.replace(/s /g, "|").replace(Words = hlWords.replace(/s /g, "|").replace(/| /greplace(/| /g , "|"); <BR>hlWords = hlWords.replace(/(^|*)|(|*$)/g, ""); <BR>if (hlWords.length == 0) return ""; <BR>var wordsArr = hlWords.split("|"); <BR>if (wordsArr.length > 1) { <BR>var resultArr = BubbleSort(wordsArr); <BR>var result = ""; for (var i = 0; i < resultArr.length; i ) { <BR>result = result "|" resultArr[i]; <BR>} <BR>return result.replace(/(^|*)|( |*$)/g, ""); <BR>} else { <BR>return hlWords; <BR>} <BR>} <BR>//利用冒泡排序法把長的關鍵字放前面<BR>function BubbleSort(arr) { <BR>var temp, exchange; <BR>for (var i = 0; i < arr.length; i ) { <BR>exchange = false; <BR>for (var j = arr .length - 2; j >= i; j--) { <BR>if ((arr[j 1].length) > (arr[j]).length) { <BR>temp = arr[j 1] ; arr[j 1] = arr[j]; arr[j] = temp; <BR>exchange = true; <BR>} <BR>} <BR>if (!exchange) break; <BR>} <BR>} <BR>return arr; <BR>} <BR>} <BR>//end <BR>function search() { <BR>var obj = document.getElementById("waiDiv"); <BR>var keyWordget = document.Element. ("keyWord"); <BR>MarkHighLight(obj, keyWord.value, "Orange"); <BR>} <BR></script> <br> <br>







二貨朋友玩遊戲被騙1200塊,報警後被告知不夠2000沒辦法立案。強大的二貨又往那個帳號寄了800塊。你說那騙子是開心呢?還是開心極了呢。






改良版

複製程式碼 程式碼如下:

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 ]);
}
}
}
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板