日期:2014-05-16 浏览次数:20416 次
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script> window.onload = function() { var cbs = document.getElementsByName("chk"); for(var i=0;i<cbs.length;i++) { if(cbs[i].checked==true) { cbs[i].parentNode.parentNode.style.backgroundColor="blue"; } } } </script> </head> <body> <table> <tr><td><input type="checkbox" name="chk" value="" checked="true"></td><td>1</td></tr> <tr><td><input type="checkbox" name="chk" value=""></td><td>2</td></tr> <tr><td><input type="checkbox" name="chk" value=""></td><td>3</td></tr> </table> </body> </html>
------解决方案--------------------
1:先在css里面定义好要用的样式:
.my_table td .firstTr_td{ background:#000}/*这里定义第一行tr内td的样式*/
.my_table td{ background:#fff}/*这里定义其它tr内td的样式*/
2:实现erase()函数
function erase()
{
document.getElementById("my_table").className = "my_table";
}
3:HTML如下:
<table id="my_table">
<tr><td class="firstTr_td"><input onclick="erase()"/></td></tr>
<tr><td></td>....</tr> <!-- 这里有10个td -->
</table>
这种方法充分利用了CSS和JS的结合,简单方便
------解决方案--------------------
<!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/html; charset=gb2312" /> <title>无标题文档</title> <script type="text/javascript" language="javascript"> function erase() { var tblOne = document.getElementById("tblOne"); for(var nowindex = 1; nowindex < tblOne.rows.length; nowindex++ ) { tblOne.rows[nowindex].bgColor = "#FFFFFF"; } } </script> </head> <body> <form name="frmtable"> <table id = "tblOne" width="50%" border="1" height="50%" bgcolor="red" bordercolor="#FFFFFF"> <tr height="35px"> <td colspan="10"> <input type="button" value="改变颜色" onclick="erase()" /> </td> </tr> <tr height="35px"> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> <td></td> </tr> <tr height="35px"> <td></td>