<table id="aaa" >
<tr >
<td ><input type="checkBox" id="a1"> </td>
<td width="130" > <input type="checkBox"> </td>
<td width="180" ><input type="checkBox"> </td>
<td align="left" > <span>44444444 </span></td>
</tr>
<tr >
<td ><input type="checkBox" id="a2"> </td>
<td width="130" > <input type="checkBox"> </td>
<td width="180" ><input type="checkBox"> </td>
<td align="left" > <span>5555 </span></td>
</tr>
</table>
<script language=javascript>//var aa=document.getElementById("aaa")
//var i=0; //0 cell
////alert(aa.rows.length)
//for(var m=0;m<aa.rows.length;m++){
// if(aa.rows[m].cells[i].childNodes[0].type=="checkbox"){
// alert(aa.rows[m].cells[i].innerHTML)
// alert(aa.rows[m].cells[i].childNodes[0].type)
// }
//}
function changeColor(){
// var arr=document.getElementById("state"); //state是td的ID //document.getElementById("")取到的不是数组
var arr=document.getElementById("aaa").childNodes[0].childNodes[0].childNodes[3];
if(arr.innerText="44444444"){
arr.style.cssText="font-weight:bold;color:red;";
}
else if(arr.innerText="可供"){
arr.style.cssText="font-weight:bold;color:blue;";
}
}
changeColor();
</script>
<tr >
<td ><input type="checkBox" id="a1"> </td>
<td width="130" > <input type="checkBox"> </td>
<td width="180" ><input type="checkBox"> </td>
<td align="left" > <span>44444444 </span></td>
</tr>
<tr >
<td ><input type="checkBox" id="a2"> </td>
<td width="130" > <input type="checkBox"> </td>
<td width="180" ><input type="checkBox"> </td>
<td align="left" > <span>5555 </span></td>
</tr>
</table>
<script language=javascript>//var aa=document.getElementById("aaa")
//var i=0; //0 cell
////alert(aa.rows.length)
//for(var m=0;m<aa.rows.length;m++){
// if(aa.rows[m].cells[i].childNodes[0].type=="checkbox"){
// alert(aa.rows[m].cells[i].innerHTML)
// alert(aa.rows[m].cells[i].childNodes[0].type)
// }
//}
function changeColor(){
// var arr=document.getElementById("state"); //state是td的ID //document.getElementById("")取到的不是数组
var arr=document.getElementById("aaa").childNodes[0].childNodes[0].childNodes[3];
if(arr.innerText="44444444"){
arr.style.cssText="font-weight:bold;color:red;";
}
else if(arr.innerText="可供"){
arr.style.cssText="font-weight:bold;color:blue;";
}
}
changeColor();
</script>
<head>
<script language=javascript>
function setcolor(obj){
switch(obj.innerText){
case "red":
obj.style.cssText="font-weight:bold;color:red;";
break;
case "blue":
obj.style.cssText="font-weight:bold;color:blue;";
break;
case "black":
obj.style.cssText="font-weight:bold;color:black;";
break;
}}
</script> </head>
<form>
<body><table id="tab1" >
<tr>
<td onclick="setcolor(this)">red</td>
<td onclick="setcolor(this)">blue</td>
<td onclick="setcolor(this)">black</td>
</tr>
</table>
</body>
</form>
</html>
如果所有的td都要判断用上面的
注意,下面的td的id和name属性都要有,因为在FF浏览器里使用DOM2标准,使用name属性,而IE里用id <table id="tab" border=1>
<tr>
<td>占用</td><td id="state" name="state">可供</td>
<tr>
<tr>
<td>可供</td><td id="state" name="state">占用</td>
<tr>
<tr>
<td>占用</td><td id="state" name="state">可供</td>
<tr>
<tr>
<td>可供</td><td id="state" name="state">占用</td>
<tr>
</table>
<input type="button" value="change" onclick="changeColor();">
<script>
//自定义去空格方法
String.prototype.trim=function(){
return this.replace(/^\s*/g,"").replace(/\s*$/g,"");
}
function changeColor(){
//var arr=document.getElementById("tab").getElementsByTagName("td");//tab是table的ID
var arr=document.getElementsByName("state");//state是td的name属性
for(var i=0;i<arr.length;i++){
if(arr[i].firstChild.data.trim()=="占用"){
arr[i].style.cssText="font-weight:bold;color:red;";
}else if(arr[i].firstChild.data.trim()=="可供"){
arr[i].style.cssText="font-weight:bold;color:blue;";
}
}
}
</script>