在firefox中使用element.style.color
Using element.style.color within firefox
我有下面的JavaScript函数,它可以更改元素中的文本,作为一种家务劳动,我还希望确保文本可读。因此,我为深色背景设置了白色,否则设置为黑色。
现在,该代码在IE9和Chrome上可以正常工作,但由于某些原因,无法正确更改Firefox上的颜色属性。为什么会这样?Firefox是否不支持我访问颜色属性的方式?
function setTextContent(element, text) {
while (element.firstChild!==null)
element.removeChild(element.firstChild); // remove all existing content
element.appendChild(document.createTextNode(text));
if (element.style.backgroundColor == "black"
|| element.style.backgroundColor == "purple"
|| element.style.backgroundColor == "blue") {
element.style.color = "white";
} else {
element.style.color = "black";
}
}
为什么不使用类?
.blackBackground {
background-color: black;
color: white;
}
.purpleBackground {
background-color: purple;
color: white;
}
等等
啊,所以最后的问题是区分大小写。
"黑色"不等于"黑色"。应该意识到这一点:)
想想,element.firstChild
会变成null
吗?它会被设置为一个空的文本节点或类似的东西吗?
当条件类似时进行检查
while (element.firstChild!==null)//wrong
它应该像
while (element.firstChild!=null)
或简称
while (element.firstChild)
相关文章:
- 为什么当我分配给element.style时,我的元素的样式没有改变
- 可以't在Firefox中设置element.style.float
- 设置 $(element).style.backgroundColor,然后检查但不起作用
- 停用“;element.style”;在css/html中
- Javascript element.style.backgroundImage 未加载图像
- Javascript viewport then element.style.left 不起作用?这有什么问题
- Javascript element.style.height 不起作用
- 如何使用 Element.style.styleFloat
- element.style.fontSize does not return size
- 写入 element.style.visibility 对页面没有影响(相同的 ID 有不同的地址?)
- element.style.display='none' 不起作用
- 在firefox中使用element.style.color
- Javascript-element.style.left返回空白
- 如果display是在CSS上声明的,为什么element.style.display是空的
- 当它说element.style时,如何找到代码的来源
- JavaScript compare Element.style.display
- 如何设置element.style.left和element.syle.top
- 使用DOM's element.style获取原始样式名称
- 为什么在这个 JavaScript 片段中忽略了 element.style
- 当使用 element.style = <一些 CSSStyleDeclaration> 时,会使元素消失