javascript - 如何获取TD的BGCOLOR?

标签 javascript html

如何获取bgcolor的值? (我的警报是错误的)。

<table id="mytable1" onclick="setColor()" width="25" border-color:black border="1" cellspacing="1" cellpading="0" align="left">
    <tr>
          <td id='colorId' bgcolor=yellow>&nbsp;</td>
    </tr>
</table>

<script type="text/javascript">
alert(document.getElementById("colorId").GetAttribute('bgcolor'));
</script>

最佳答案

Javascript 区分大小写。 getAttribute() 应使用小写的 g 编写(如 getElementById 和所有其他)。这称为驼峰命名法(或驼峰命名法,然后名称说明它是什么),Javascript 函数遵循此命名约定。

document.getElementById("colorId").getAttribute('bgcolor')

此外,您不能将 CSS 直接写入 HTML 元素,您必须使用 style 属性:

style="border-color: black;"

还有一件事,如果你允许的话。尽量保持标记风格一致。在 HTML4/5 中,您可以在属性值周围使用 '" 或不使用任何内容(例如 id="colorId"id ='colorId'id=colorId),您应该在同一 HTML 页面中坚持使用其中一个(并不是说您不能混合使用它们,但保持一致被认为是更好的做法) .

关于javascript - 如何获取TD的BGCOLOR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5834432/

相关文章:

javascript - 如何清除一个IFRAME的内容?

导致 PIE.htc 错误的 Javascript/Google map

html - 如何在 IE7 中更改边框颜色?

jquery - 如何使用 CSS 将 "+"旋转为 "x"?

javascript - 如何使用 UL 和 li 构建表状结构

python - 如何在 Python 中使用 Selenium 获取 h1 标签

带有 display 属性的 JavaScript 转换?

javascript - react setState 替换错误的值

javascript - 在数组中添加两行相同的行而不是仅修改其中一行

php - 表单中可变数量的输入字段