<分区>
标签 javascript html css
<分区>
我使用以下功能将我的“时钟网站”更改为更轻的主题。
var light = function() {
document.getElementsByClassName('themechange').style.color = "black"
document.getElementsByTagName('body').style.backgroundColor = "white";
};
当我尝试运行函数时,标题中出现错误。如果您需要更多代码,请检查元素 here
最佳答案
两者都是Document.getElementsByClassName()
和 Document.getElementsByTagName()
返回 NodeLists
对象。
您不能将设置直接应用于 NodeList
,您必须遍历它并将设置应用于每个 Node
.
但是您可以使用 Document.body
直接引用 body
元素。
Document.getElementsByClassName()
是not supported in Internet Explorer 8但是Document.querySelectorAll()是supported .
/* Select all elements with the class `themechange`
* Iterate with a for loop
* Set each element's background color to `black`
* Set the body's background color to `white`
*/
var light = function() {
var themeChanges = document.querySelectorAll('.themechange'), themeChange, i;
for(i = 0; (themeChange = themeChanges[i]); i++) {
themeChange.style.color = "black"
}
document.body.style.backgroundColor = "white";
};
关于javascript - "Cannot set color of undefined"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30657376/