日期:2014-05-16  浏览次数:20349 次

js根据td的值动态修改td颜色
做了个DEMO,要求是如果表中某个值超出指定值,那个这个td就变没红色,以下是代码

<html>
<head>
            
</head>
<body onload="onload()">
<table id=t1 align="center" border="1">
<tr>
<td>第一行</td>
<td>11</td>
</tr>
<tr>
<td>第二行</td>
<td>22</td>
</tr>
<tr>
<td>第三行</td>
<td>33</td>
</tr>
<tr>
<td>第四行</td>
<td>44</td>
</tr>
</table>

<script type="text/javascript"> 
                function onload(){ 
		    var danger = 25;
		    var table = document.getElementById("t1")
                    //var table = document.getElementById("t1").rows[0].cells[0]; 
                    var rowObj = null; 
                    var cellObj = null;     
                     
                    // 引用rows 
                    for (var i = 0; i < table.rows.length; i ++){ 
                        rowObj = table.rows[i];                        
			var s = rowObj.cells[1].innerHTML;			
			if(s>=danger){
				//rowObj.cells[1].style.color='red';
				rowObj.cells[1].style.backgroundColor="red";;
			}

                    } 
                } 
</script>
</body>
</html>