javascript - 无法更改 TD 的 borderColor

标签 javascript css firefox colors border

用JS设置TD的背景色就可以了。但是,在 FF 3.0.18 中设置边框颜色是有问题的,尽管 IE 6 没有遇到这个问题。 FF 存在问题,因为它要求 TD 元素将 style 属性初始化为 border-style: solid。没有它,设置 TD 的边框颜色将不起作用。这是已知错误吗?

如何在不设置 style 属性和初始化值的情况下设置边框颜色?

我知道设置 class 属性而不是直接设置边框颜色的另一个技巧。这是否表明 TD 不喜欢动态设置其边框颜色?这也是众所周知的吗?

下面是有问题的代码(目标是找出为什么设置边框颜色 simple truth 1 不起作用,而 simple truth 3 在我使用该技巧时起作用如上所述):

<html>
<head>
<title>Quirks FF 3.0.18</title>
<style type="text/css">
table {
    border-collapse: collapse;
}
</style>
<script type="text/javascript">
function changeBgColor()
{
    document.getElementById('simple').style.backgroundColor='yellow';
    document.getElementById('simple2').style.backgroundColor='yellow';
    document.getElementById('simple3').style.backgroundColor='yellow';
}

function quirk(id)
{
    var x = document.getElementById(id);

    x.style.border = '2px solid red';
}
</script>
</head>
<body>
    <input type="button" onclick="changeBgColor()" value="Change background color"/>
    <input type="button" onclick="quirk('simple')" value="Change border color 1"/>
    <input type="button" onclick="quirk('simple2')" value="Change border color 2"/>
    <input type="button" onclick="quirk('simple3')" value="Change border color 3"/>
    <table>
    <tr><td id="simple">Simple truth 1</td></tr>
    </table>
    <table>
    <tr><td><span id="simple2">Simple truth 2</span></td></tr>
    <table>
    <tr><td id="simple3" style="border-style: solid">Simple truth 3</td></tr>
    </table>
</body>
</html>

最佳答案

我认为这是一个错误。正如您已经指出的那样,解决方法是将 border-style 设置为 solid。您可以将它添加到样式表中,以便始终初始化 TD:

TD { border-style: solid;

关于javascript - 无法更改 TD 的 borderColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2371713/

相关文章:

javascript - 绝对定位的视差元素导致页面高度过大

html - 使用 css 激活下拉菜单

javascript - 我可以在 Firefox 扩展的弹出通知中设置背景颜色吗?

javascript - 通过 Javascript 作为关键帧替代来操纵 CSS 动画类的方法是什么

firefox - HTML5 - 在 <a> 中包含 <li> Firefox 问题?

html - Firefox SVG 图形模糊

javascript - AngularJS 外部 javascript 库不运行

javascript - 禁用焦点 - jQuery

Javascript循环函数(i)

html - 使用背景图标拆分 css 边框