<! 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>
<title>js 查詢表格內容並加亮相應內容儲存格</title>
<meta HTTP-equiv="content-type" content="text/html;charset=gb2312">
<!--把下面代碼加到<head>與</head>之間-->
<script language="javascript">
var ids;
var idstext;
function hightlight_td(){
var text=txt.value;
TDs=document.all.table1.all.tags("td")
//使用iframe時:
//TDs=iframe的name.document.all.table1.all.tags("TD")
if(text!=""){
for (var i=0;i<TDs.length;i++){
obj=TDs[i];
idstext=obj.innerText;
idnum=idstext.indexOf(text);
if(idnum!=-1){
obj.style.background="red";
}else{
obj.style.background="#FFFFFF";
}
}
}else{
alert("請輸入要查詢的字串! ");
}
}
</script>
</head>
<body>
<!--把下面代碼加到<body>與</body>之間-->
<table border="1" cellspacing="0" width="400" cellpadding="0" id="table1">
<tr>
<td width="80">12</td>
<td width="80">34</td>
<td width="80">56</td>
<td width="80">78</td>
<td width="80">90</td>
</tr>
<tr>
<td width="80">abc</td>
<td width="80">def</td>
<td width="80">ghj</td>
<td width="80">lop</td>
<td width="80">dry</td>
</tr>
<tr>
<td width="80">!@#</td>
<td width="80">_(*</td>
<td width="80">#$%</td>
<td width="80">^$#</td>
<td width="80">WWW</td>
</tr>
</table>
<input type="text" name="txt"><input type="button" value="請輸入要查詢的字串,例如12" onClick="hightlight_td();" >
</body>
</html>