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

怎么改变表格中单元格的颜色?
下面的代码为什么没有改变单元格的颜色呢?
<html>
<head>
<title> 无标题文档 </title>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
</head>

<body   bgcolor= "#FFFFFF "   text= "#000000 ">
<input   type= "submit "   name= "Submit "   value= "提交 "   onclick= "che() ">

<script   language= "javascript ">
            function   che(){
var   Begin_row,Begin_cell,Begin_txtNode;
                var     completeTable   =   document.getElementById( "complete_table ");
                alert(completeTable.innerHTML);
                var   completeBody   =   document.getElementById( "complete_body ");
                      Begin_row   =   document.createElement( "tr ");
                                Begin_cell   =   document.createElement( "td ");
                                Begin_cell.setAttribute( "bgcolor ", "#00FF66 ");
                                Begin_txtNode   =   document.createTextNode( "Please     Input ");
                                Begin_cell.appendChild(Begin_txtNode);
                                Begin_row.appendChild(Begin_cell);
                                completeBody.appendChild(Begin_row);
  alert(completeTable.innerHTML);
}
</script>
<table   id= "complete_table "     border= "2 "   cellspacing= "0 "   cellpadding= "0 "   width= "8 "   height= "8 "/>
    <tbody   id= "complete_body "> </tbody>  
</table>
</body>
</html>
      代码没错,但是为什么改变不了颜色呢?

------解决方案--------------------
Begin_cell.setAttribute( "bgcolor ", "#00ccff ");
============>
Begin_cell.setAttribute( "bgColor ", "#00ccff ");
把bgcolor改为bgColor
用js来控制标签的时候,是严格大小写的
------解决方案--------------------
可以用style方式来设置,不必setAttribute
Begin_cell.style.bgColor = " ";