How do I change the bgcolor of 'colorId'. I tried the code below and I want the bgcolor to change in the value 'val'. But from then开发者_运维问答 on I m doing something wrong.
<script type="text/javascript">
function updatevariable(elm) {
val = elm;
var divElement = document.getElementById(colorId);
divElement.bgcolor = val;
}
</script>
<table width="150" border="0" cellspacing="1" cellpading="0" align="center">
<tr>
<td bgcolor="#190707" onclick="updatevariable(this.bgColor)"> </td>
<td bgcolor="#fa5858" onclick="updatevariable(this.bgColor)"> </td>
<td bgcolor="#F4FA58" onclick="updatevariable(this.bgColor)"> </td>
<td bgcolor="#00FF00" onclick="updatevariable(this.bgColor)"> </td>
<td bgcolor="#fbefef" onclick="updatevariable(this.bgColor)"> </td>
</tr>
<tr>
<td id='colorId' bgcolor="#F4FA58"> </td>
</tr>
</table>
If you switch to jQuery you can do this in stead:
function updatevariable(elm){
var val = elm;
$('#colorId').css('background-color', val);
}
Just found your bugs. Use code below and it would work.
function updatevariable(elm) {
var val = elm;
var divElement = document.getElementById("colorId");
divElement.setAttribute("bgcolor", val);
}
精彩评论