做getElementsByClassName(等类似的功能getElementsByTagName和querySelectorAll)的工作方式相同getElementById,还是他们返回元素的数组?
我问的原因是因为我试图改变所有元素的样式getElementsByClassName.见下文.
//doesn't work
document.getElementsByClassName('myElement').style.size = '100px';
//works
document.getElementById('myIdElement').style.size = '100px';
Run Code Online (Sandbox Code Playgroud) 我有以下代码,我想获取元素所需的 CSS 属性(即使样式是由 js 设置的,即由 SPA 创建的文件,如 React),而不是计算/应用的属性。例如computedStyle.width应该有一个未定义的值。我怎样才能做到这一点?
let ele = document.querySelector(".hello");
let computedStyle = window.getComputedStyle(ele);
console.log(computedStyle.backgroundColor);
console.log(computedStyle.height);
console.log(computedStyle.width); // <- This should be undefined or ""Run Code Online (Sandbox Code Playgroud)
.hello {
height: 10px;
background-color: red;
}Run Code Online (Sandbox Code Playgroud)
<div class="hello">
helloInnerText
</div>Run Code Online (Sandbox Code Playgroud)