<div class="codetitle"> <span><a style="CURSOR: pointer" data="87810" class="copybut" id="copybut87810" onclick="doCopy('code87810')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code87810"> <br><!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <br><html xmlns="http://www.w3.org/1999/xhtml"> <br><head> <br><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <br><title>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; <br>if (bgColor == null || bgColor == "") { <br>bgColor = "red"; <br>} <br>MarkHighLightCore(obj, hlWords); <br>//执行高亮标记的核心方法 <br>function MarkHighLightCore(obj, 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.innerHTML = childObj.data.replace(reResult, "<span" style ">$1</span>"); <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>} <br>//分析关键词 <br>function AnalyzeHighLightWords(hlWords) { <br>if (hlWords == null) return ""; <br>hlWords = hlWords.replace(/s /g, "|").replace(/| /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 = ""; <br>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>return arr; <br>} <br>} <br>//end <br>function search() { <br>var obj = document.getElementById("waiDiv"); <br>var keyWord = document.getElementById("keyWord"); <br>MarkHighLight(obj, keyWord.value, "Orange"); <br>} <br></script> <br></head> <br><body> <br><div id="waiDiv"> <br><input type="text" id="keyWord" /> <br><input type="button" value="搜索" onclick="search()" /><br /> <br><br /> <br><div id="contentDiv"> <br>二货朋友玩游戏被骗1200块,报警后被告知不够2000没办法立案。强大的二货又往那个账号寄了800块。你说那骗子是开心呢?还是开心极了呢。 <br></div> <br></div> <br></body> <br></html> <br> </div> <br>改进版 <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="79524" class="copybut" id="copybut79524" onclick="doCopy('code79524')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code79524"> <br>functionhighlightWord(node, word) { <br>// 迭代到此节点 childNodes <br>if (node.hasChildNodes) { <br>var hi_cn; <br>for (hi_cn = 0; hi_cn highlightWord(node.childNodes[hi_cn], word); <br>} <br>} <br>// 执行此节点本身 <br>if (node.nodeType == 3) { // 文本节点 <br>tempNodeVal = node.nodeValue.toLowerCase(); <br>tempWordVal = word.toLowerCase(); <br>if (tempNodeVal.indexOf(tempWordVal) != -1) { <br>pn = node.parentNode; <br>if (pn.className != "highlight") { <br>// 单词尚未突出显示! <br>nv = node.nodeValue; <br>ni = tempNodeVal.indexOf(tempWordVal); <br>// 创建替换节点负载 <br>before = document.createTextNode(nv.substr(0, ni)); <br>docWordVal = nv.substr(ni, word.length); <br>after = document.createTextNode(nv.substr(ni word.length)); <br>hiwordtext = document.createTextNode(docWordVal); <br>hiword = document.createElement("span"); <br>hiword.className = "突出显示"; <br>hiword.appendChild(hiwordtext); <br>pn.insertBefore(之前,节点); <br>pn.insertBefore(hiword, 节点); <br>pn.insertBefore(之后,节点); <br>pn.removeChild(节点); <br>} <br>} <br>} <br>} <br>//根据Tag名高亮关键字 <br>function SearchHighlightTag(node, key) { <br>if (!document.createElement) return ; <br>if (key.length === 0) 返回 false; <br>var 数组 = new Array(); <br>array = key.split(" "); <br>var element = document.getElementsByTagName(node); <br>for (var i = 0; i for (var j = 0; j highlightWord(element[j],数组[i]); <br>} <br>} <br>} <br>//根据ID高亮关键字 <br>function SearchHighlightID(node, key) { <br>if (!document.createElement) return; <br>if (key.length === 0) 返回 false; <br>var 数组 = new Array(); <br>array = key.split(" "); <br>var element = document.getElementById(node); <br>for (var i = 0; i for (var j = 0; j highlightWord(element, array[i ]); <br>} <br>} <br>} <br> </div>