<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()"> </td>
<td width="13" bgcolor="rgb(153, 51, 0)" onclick="getColor()" title="褐色"> </td>
<td width="13" bgcolor="rgb(51, 51, 0)" onclick="getColor()" title="橄榄色"> </td>
<td width="13" bgcolor="rgb(0, 51, 0)" onclick="getColor()" title="深绿"> </td>
<td width="13" bgcolor="rgb(0, 51, 102)" onclick="getColor()" title="深青"> </td>
<td width="13" bgcolor="rgb(0, 0, 128)" onclick="getColor()" title="深蓝"> </td>
<td width="13" bgcolor="rgb(51, 51, 153)" onclick="getColor()" title="靛蓝"> </td>
<td width="13" bgcolor="rgb(51, 51, 51)" onclick="getColor()" title="灰色-80%"> </td>
</tr>
</table>
<input name="inputColor" class="text" id="inputColor" value="#" size="7" maxlength="7" />
怎么取得这个表格的td的背景色啊?
这段代码运行后没反应,哪边错了?应该怎么修改?
function getColor(){
..
this.getAttribute("bgcolor");//
..
}
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)"> </td>
<td width="13" bgcolor="rgb(153, 51, 0)" onclick="getColor(this)" title="褐色"> </td>
<td width="13" bgcolor="rgb(51, 51, 0)" onclick="getColor(this)" title="橄榄色"> </td>
<td width="13" bgcolor="rgb(0, 51, 0)" onclick="getColor(this)" title="深绿"> </td>
<td width="13" bgcolor="rgb(0, 51, 102)" onclick="getColor(this)" title="深青"> </td>
<td width="13" bgcolor="rgb(0, 0, 128)" onclick="getColor(this)" title="深蓝"> </td>
<td width="13" bgcolor="rgb(51, 51, 153)" onclick="getColor(this)" title="靛蓝"> </td>
<td width="13" bgcolor="rgb(51, 51, 51)" onclick="getColor(this)" title="灰色-80%"> </td>
</tr>
</table>
<input name="inputColor" class="text" id="inputColor" value="#" size="7" maxlength="7" />
<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)"> </td>
<td width="13" bgcolor="rgb(153, 51, 0)" onclick="getColor(this)" title="褐色"> </td>
<td width="13" bgcolor="rgb(51, 51, 0)" onclick="getColor(this)" title="橄榄色"> </td>
<td width="13" bgcolor="rgb(0, 51, 0)" onclick="getColor(this)" title="深绿"> </td>
<td width="13" bgcolor="rgb(0, 51, 102)" onclick="getColor(this)" title="深青"> </td>
<td width="13" bgcolor="rgb(0, 0, 128)" onclick="getColor(this)" title="深蓝"> </td>
<td width="13" bgcolor="rgb(51, 51, 153)" onclick="getColor(this)" title="靛蓝"> </td>
<td width="13" bgcolor="rgb(51, 51, 51)" onclick="getColor(this)" title="灰色-80%"> </td>
</tr>
</table>
<input name="inputColor" class="text" id="inputColor" value="#" size="7" maxlength="7" />
楼上都正解,
2楼通用于IE、FF,
IE的话用3楼的就OK