JS怎么用关键字来过滤表格

JavaScript027

JS怎么用关键字来过滤表格,第1张

<!DOCTYPE html>

<html>

  <head>

<meta http-equiv="Content-Type" content="text/html charset=UTF-8">

<title>RunJS 演示代码</title>

<script>

var chazhao = function (){

var trs = tab1.tBodies[0].rows

for(var i = 0 i < trs.length i++){

var tr = trs[i]

if(tr.innerHTML.search(oTXT.value) == -1)

tr.style.display = 'none'

else

tr.style.display = ''

}

}

</script>

  </head>

<body>

     <form>

        <span>输入查询的关键字:</span>

  <input type="text" id="oTXT" />

  <input type="button" value="查找" onclick="chazhao()" />

    </form>

    <table border="1px" id="tab1" width="500">

        <thead>

        <tr>

            <td>姓名</td>

            <td>职位</td>

            <td>手机</td>

            <td>分机</td>

        </tr>

        </thead>

        <tbody>

        <tr>

            <td>111</td>

            <td>222</td>

            <td>333</td>

            <td>444</td>

        </tr>

        <tr>

            <td>589</td>

            <td>666</td>

            <td>777</td>

            <td>888</td>

        </tr>

        <tr>

            <td>567</td>

            <td>888</td>

            <td>999</td>

            <td>999</td>

        </tr>

        </tbody>

    </table>

  </body>

</html>

var input='这是一大段文本'

var keys=['这是','这里是','文本','一']

var prepareKeys=function(){

if(!prepareKeys.$map){

var map={}

var maxLength=0

for(var i=0i<keys.lengthi++){

map[keys[i]]=1

maxLength=Math.max(keys[i].length,maxLength)

}

prepareKeys.$map={

map:map,

length:maxLength

}

}

return prepareKeys.$map

}

var colorKeyword=function(str){

var info=prepareKeys()

var output=[]

while(str){

var sub=str.substring(0,info.length)

str=str.substring(info.length)

while(!info.map[sub]&&sub){

str=sub.charAt(sub.length-1)+str

sub=sub.slice(0,-1)

console.log(sub)

}

console.log('color',sub)

if(sub){

output.push('#',sub,'#')

}else{

output.push(str.charAt(0))

str=str.substring(1)

}

}

return output.join('')

}

colorKeyword(input)

/*

* 通用过滤函数,参数str为需要处理的字符串,ref为参照规则

*/

function filter(str,ref){

for(var i=0i<ref.lengthi++){

str=str.replace(new RegExp(ref[i][0],"gi"),ref[i][1])

}

return str

}

/*使用方法*/

var temp="dadfa DALeweceqrcqfgdgdagaf"

temp=filter(temp,[["dalai","da。lai"],["falun","fa。lun"],["xxx","yyy"]])

/*使用示例,将下面代码保存为网页*/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=utf-8" />

<title>无标题文档</title>

<script type="text/javascript">

function $(id){return document.getElementById(id)}

/*

* 通用过滤函数,参数str为需要处理的字符串,ref为参照规则

*/

function filter(str,ref){

for(var i=0i<ref.lengthi++){

str=str.replace(new RegExp(ref[i][0],"gi"),ref[i][1])

}

return str

}

function html_f(){

var ref = new Array()

ref.push(["html","HTML文档"])

ref.push(["head","头部"])

ref.push(["meta","媒体"])

ref.push(["title","标题"])

ref.push(["link","链接"])

ref.push(["script","脚本"])

ref.push(["body","身体"])

$("output").innerText=filter($("input").innerText,ref)

}

</script>

</head>

<body>

<textarea id="input" style="width:400pxheight:500px"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312" />

<title>javascript过滤关键词不区分大小写字母的问题_百度知道 </title>

<link rel="alternate" type="application/rss+xml" title="“javascript过滤关键词不区分大小写字母的问题”的最新回答(RSS 2.0)" href="http://zhidao.baidu.com/q?ct=20&qid=144059871&pn=65535&rn=25&tn=rssqb">

<link href="/ikqb.css" rel="stylesheet" type="text/css">

<script type="text/javascript" src="/userlogin.js?213"></script>

</head>

<body></textarea>

<input type="button" value="过滤" onclick="html_f()"/>

<textarea id="output" style="width:400pxheight:500px"></textarea>

<br/>

</body>

</html>