前端过滤关键字
body | |
{ | |
font-size: 14px; | |
} | |
h3 | |
{ | |
display: inline; | |
margin: 0; | |
} | |
#spnKeyNum, #spnWordNum | |
{ | |
color: #F00; | |
} | |
#spnList a | |
{ | |
font-size: 12px; | |
color: #0000FF; | |
} | |
#spnTime | |
{ | |
color: #600; | |
} | |
textarea, #divDisplay | |
{ | |
margin: 0; | |
padding: 0; | |
font-size: 14px; | |
display: block; | |
background: #EEE; | |
border: #999 solid 1px; | |
} | |
#txtKeys | |
{ | |
width: 95%; | |
height: 100px; | |
} | |
#txtContent, #divDisplay | |
{ | |
height: 400px; | |
width: 95%; | |
} | |
#divDisplay | |
{ | |
display: none; | |
overflow: auto; | |
} | |
#divDisplay em | |
{ | |
color: #F00; | |
font-style: normal; | |
} | |
#divFoot | |
{ | |
padding: 5px; | |
width: 95%; | |
} | |
#spnLable2 | |
{ | |
margin: 5px; | |
float: left; | |
color: #690; | |
} | |
button | |
{ | |
float: left; | |
} | |
#desc | |
{ | |
color: #333; | |
float: right; | |
} | |
关键字 | |
(0个) | |
世界城市 | |
部分英文单词 | |
成语全集 | |
内容 | |
(0字) | |
搜索用时: 0ms | |
EtherDream 2009 | |
var tblRoot; | |
/* | |
* 函数: makeTree | |
* 注释: 将关键字生成一颗树 | |
*/ | |
function makeTree() | |
{ | |
var strKeys = objKeys.value; | |
var arrKeys = strKeys.split(""); | |
var tblCur = tblRoot = {}; | |
var key; | |
for(var i=0,n=arrKeys.length; i | |
{ | |
key = arrKeys[i]; | |
if(key == ';') //完成当前关键字 | |
{ | |
tblCur.end = true; | |
tblCur = tblRoot; | |
continue; | |
} | |
if(key in tblCur) //生成子节点 | |
tblCur = tblCur[key]; | |
else | |
tblCur = tblCur[key] = {}; | |
} | |
tblCur.end = true; //最后一个关键字没有分割符 | |
} | |
/* | |
* 函数: search | |
* 注释: 标记出内容中关键字的位置 | |
*/ | |
function search(content) | |
{ | |
var tblCur; | |
var i = 0; | |
var n = content.length; | |
var p, v; | |
var arrMatch = []; | |
while(i < n) | |
{ | |
tblCur = tblRoot; | |
p = i; | |
v = 0; | |
for(;;) | |
{ | |
if(!(tblCur = tblCur[content.charAt(p++)])) | |
{ | |
i++; | |
break; | |
} | |
if(tblCur.end) //找到匹配关键字 | |
v = p; | |
} | |
if(v) //最大匹配 | |
{ | |
arrMatch.push(i-1, v); | |
i = v; | |
} | |
} | |
return arrMatch; | |
} | |
var $ = function(id){return document.getElementById(id)}; | |
var objKeys = $("txtKeys"); | |
var objContent = $("txtContent"); | |
var objDisplay = $("divDisplay"); | |
var bKeyChanged = false; | |
function handleKeyChange() | |
{ | |
var str = objKeys.value; | |
var n; | |
if(str.length==0) | |
n = 0; | |
else | |
n = str.split(";").length; | |
/* | |
* 显示关键字数量 | |
*/ | |
$("spnKeyNum").innerHTML = n; | |
bKeyChanged = true; | |
} | |
function handleContentChange() | |
{ | |
/* | |
* 显示内容长度 | |
*/ | |
$("spnWordNum").innerHTML = objContent.value.length; | |
} | |
function handleLoad() | |
{ | |
handleKeyChange(); | |
handleContentChange(); | |
} | |
function handleClick() | |
{ | |
var strContent = objContent.value; | |
var arrMatch; | |
var arrHTML = []; | |
var strHTML; | |
var mid; | |
var p = 0; | |
$("btnStart").disabled = true; | |
$("btnReset").disabled = false; | |
if(bKeyChanged) | |
{ | |
makeTree(); | |
bKeyChanged = false; | |
} | |
/* | |
* 开始搜索! | |
*/ | |
var t = +new Date(); | |
arrMatch = search(strContent); | |
/* | |
* 搜索用时 | |
*/ | |
$("spnTime").innerHTML = +new Date() - t; | |
/* | |
* 标记关键字 | |
*/ | |
for(var i=0,n=arrMatch.length; i | |
{ | |
mid = arrMatch[i]; | |
arrHTML.push(strContent.substring(p, mid), | |
"", | |
strContent.substring(mid, p = arrMatch[i+1]), | |
""); | |
} | |
arrHTML.push(strContent.substring(p)); | |
strHTML = arrHTML.join("").replace(/\n/g, " "); | |
/* | |
* 显示结果 | |
*/ | |
objDisplay.innerHTML = strHTML; | |
objContent.style.display = "none"; | |
objDisplay.style.display = "block"; | |
} | |
function reset() | |
{ | |
$("btnStart").disabled = false; | |
$("btnReset").disabled = true; | |
objContent.style.display = "block"; | |
objDisplay.style.display = "none"; | |
} | |
function load(file) | |
{ | |
/* | |
* 创建HTTP组件 | |
*/ | |
if(window.ActiveXObject) | |
{ | |
objHttp = new ActiveXObject("Microsoft.XMLHTTP"); | |
} | |
else | |
{ | |
objHttp = new XMLHttpRequest(); | |
objHttp.overrideMimeType("text/xml"); | |
} | |
objHttp.onreadystatechange = function() | |
{ | |
if(objHttp.readyState != 4) | |
return; | |
objKeys.value = objHttp.responseText; | |
handleKeyChange(); | |
}; | |
/* | |
* 发送请求 | |
*/ | |
objHttp.open("GET", file, true); | |
objHttp.send(null); | |
} | |
网站栏目:前端过滤关键字
文章来源:http://myzitong.com/article/joegii.html