<script   language="javascript">   
  function   getColor()   
  {     
        var a = document.getElementById("colorP").cell.getAttribute("bgcolor");//怎么取得单元格的背景颜色
document.getElementById("inputColor").value = a; 
  }   
</script>   
 
<table   border="1"  width="100%" id="colorP">   
         <tr>
        <td width="13" bgcolor="rgb(0, 0, 0)" title="黑色" onm onclick="getColor()">&nbsp;</td>
        <td width="13" bgcolor="rgb(153, 51, 0)" onclick="getColor()" title="褐色">&nbsp;</td>
        <td width="13" bgcolor="rgb(51, 51, 0)" onclick="getColor()" title="橄榄色">&nbsp;</td>
        <td width="13" bgcolor="rgb(0, 51, 0)" onclick="getColor()" title="深绿">&nbsp;</td>
        <td width="13" bgcolor="rgb(0, 51, 102)" onclick="getColor()" title="深青">&nbsp;</td>
        <td width="13" bgcolor="rgb(0, 0, 128)" onclick="getColor()" title="深蓝">&nbsp;</td>
        <td width="13" bgcolor="rgb(51, 51, 153)" onclick="getColor()" title="靛蓝">&nbsp;</td>
        <td width="13" bgcolor="rgb(51, 51, 51)" onclick="getColor()" title="灰色-80%">&nbsp;</td>
      </tr>
  </table>   
<input name="inputColor" class="text" id="inputColor" value="#" size="7" maxlength="7" />
怎么取得这个表格的td的背景色啊?
这段代码运行后没反应,哪边错了?应该怎么修改?

解决方案 »

  1.   

    onclick="getColor(this)" 
    function getColor(){
    ..
    this.getAttribute("bgcolor");//
    ..
    }
      

  2.   

    <script   language="javascript">   
      function   getColor(o)   
      {     
            var a = o.getAttribute("bgcolor");//怎么取得单元格的背景颜色
        document.getElementById("inputColor").value = a; 
      }   
    </script>   
     
    <table   border="1"  width="100%" id="colorP">   
             <tr>
            <td width="13" bgcolor="rgb(0, 0, 0)" title="黑色" onm onclick="getColor(this)">&nbsp;</td>
            <td width="13" bgcolor="rgb(153, 51, 0)" onclick="getColor(this)" title="褐色">&nbsp;</td>
            <td width="13" bgcolor="rgb(51, 51, 0)" onclick="getColor(this)" title="橄榄色">&nbsp;</td>
            <td width="13" bgcolor="rgb(0, 51, 0)" onclick="getColor(this)" title="深绿">&nbsp;</td>
            <td width="13" bgcolor="rgb(0, 51, 102)" onclick="getColor(this)" title="深青">&nbsp;</td>
            <td width="13" bgcolor="rgb(0, 0, 128)" onclick="getColor(this)" title="深蓝">&nbsp;</td>
            <td width="13" bgcolor="rgb(51, 51, 153)" onclick="getColor(this)" title="靛蓝">&nbsp;</td>
            <td width="13" bgcolor="rgb(51, 51, 51)" onclick="getColor(this)" title="灰色-80%">&nbsp;</td>
          </tr>
      </table>   
    <input name="inputColor" class="text" id="inputColor" value="#" size="7" maxlength="7" />
      

  3.   


    <script   language="javascript">   
      function   getColor(o)   
      {         document.getElementById("inputColor").value = o.bgColor; 
      }   
    </script>   
     
    <table   border="1"  width="100%" id="colorP">   
             <tr>
            <td width="13" bgcolor="rgb(0, 0, 0)" title="黑色" onm onclick="getColor(this)">&nbsp;</td>
            <td width="13" bgcolor="rgb(153, 51, 0)" onclick="getColor(this)" title="褐色">&nbsp;</td>
            <td width="13" bgcolor="rgb(51, 51, 0)" onclick="getColor(this)" title="橄榄色">&nbsp;</td>
            <td width="13" bgcolor="rgb(0, 51, 0)" onclick="getColor(this)" title="深绿">&nbsp;</td>
            <td width="13" bgcolor="rgb(0, 51, 102)" onclick="getColor(this)" title="深青">&nbsp;</td>
            <td width="13" bgcolor="rgb(0, 0, 128)" onclick="getColor(this)" title="深蓝">&nbsp;</td>
            <td width="13" bgcolor="rgb(51, 51, 153)" onclick="getColor(this)" title="靛蓝">&nbsp;</td>
            <td width="13" bgcolor="rgb(51, 51, 51)" onclick="getColor(this)" title="灰色-80%">&nbsp;</td>
          </tr>
      </table>   
    <input name="inputColor" class="text" id="inputColor" value="#" size="7" maxlength="7" />
      

  4.   

    LZ的问题在于没有指定cell,并不是getAttribute("bgcolor")的问题。
    楼上都正解,
    2楼通用于IE、FF,
    IE的话用3楼的就OK