How do I get the value of bgcolor? (my alert is wrong).
<table id="mytable1" onclick="setColor()" width="25" border-color:black border="1" cellspacing="1" cellpading="0" align="left">
<tr>
<td id='colorId' bgcolor=yellow> </td>
</tr>
</table>
<script type="text/javascript">
alert(document.getElementById("colorId").GetAttri开发者_JAVA百科bute('bgcolor'));
</script>
Javascript is case-sensitive. getAttribute()
should be written with a lowercase g
(like getElementById
and all others). This is called camel-case (or camelCase and then the name says what it is), Javascript functions follow this naming convention.
document.getElementById("colorId").getAttribute('bgcolor')
Also you can't write CSS directly to a HTML element, you have to use the style
attribute:
style="border-color: black;"
One more thing, if you let me. Try to be consistent in your markup style. In HTML4/5 you can use either '
, "
or nothing around your attribute values (like id="colorId"
, id='colorId'
or id=colorId
), you should stick to one of these in the same HTML page (not that you cannot mix them, but staying consistent is considered a better practice).
getAttribute
not GetAttribute
精彩评论