如何在javascript中编写css代码?(未捕获的TypeError:无法设置undefined的属性"height")

NEW*_*NEW 2 javascript css

如何在javascript中编写css代码?(未捕获的TypeError:无法设置undefined的属性"height")

JavaScript的

document.getElementById("slideshow").getElementsByClassName("arrow").style.height = "86px";
Run Code Online (Sandbox Code Playgroud)

CSS

#slideshow .arrow{
    height:86px;
    width:60px;
    position:absolute;
    background:url('arrows.png') no-repeat;
    top:50%;
    margin-top: -43px;
    cursor: pointer;
    z-index: 5000;
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*lly 6

这里的关键是getElementsByClassName- 元素的复数.此方法返回元素的类似数组的对象,而不仅仅是一个元素.

要将样式应用于每个样式,您需要遍历此类似数组的对象并将样式添加到返回的每个单独元素:

var elems = document.getElementById("slideshow").getElementsByClassName("arrow");

for (var i = 0; i < elems.length; i++)
    elems[i].style.height = "86px";
Run Code Online (Sandbox Code Playgroud)