您现在的位置是:首页 > 电脑技术查询 > web开发

怎么用js实现对一个表格的td改变颜色,小弟我这样写为什么不对

编辑:chaxungu时间:2022-10-02 23:21:17分类:web开发

如何用js实现对一个表格的td改变颜色,我这样写为什么不对?
<html> <head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> 无标题文档 </title>
</head>
<script>
function fu(){
document.getElementById( 'yt ').style.bgColor = '#FF0000 ';
}
</script>
<body>
<input type= "button " name= "Submit " onClick= "fu() " value= "提交 ">
<table width= "75% " border= "1 ">
<tr>
<td height= "20 " id= "yt " > &nbsp; </td>
<td bgcolor= "#FF0000 "> &nbsp; </td>
</tr>
</table>
</body> </html>

------解决方案--------------------
修改1.
function fu(){
document.getElementById( 'yt ').style.background = '#FF0000 ';
}
修改2.增加一個div

<td height= "20 " > <div id= "yt "> &nbsp; </div> </td>
<td bgcolor= "#FF0000 "> &nbsp; </td>

------解决方案--------------------
document.getElementById( 'yt ').style.background = '#FF0000 ';
------解决方案--------------------
<table> <tr> <td id= 'fff '> fffffff </td> </tr> </table>
<script>
document.all( "fff ").bgColor = "red "
document.all( "fff ").style.backgroundColor = "blue "
</script>

两种方法都可以
------解决方案--------------------
UP楼上的~
bgColor是一属性,不在style下面的~
background-color才是style下面的属性~但在脚本下面,要写成backgroundColor