Javascript 如何使用clientWidth获取元素的宽度值?
我无法返回元素宽度的值Javascript 如何使用clientWidth获取元素的宽度值?,javascript,width,console.log,Javascript,Width,Console.log,我无法返回元素宽度的值 let dimensionPopup1 = popup.getElementsByTagName("img"); console.log(dimensionPopup1.clientWidth); 它在控制台中返回“未定义”。与offsetWidth、scrollWidth和width相同。 当我在控制台中检查dimensionPopup1时,我会看到所有元素及其值。 我做错了什么?返回一个元素数组,因此必须访问数组中的各个元素才能获得它们的属性 例如 var ite
let dimensionPopup1 = popup.getElementsByTagName("img");
console.log(dimensionPopup1.clientWidth);
它在控制台中返回“未定义”。与offsetWidth、scrollWidth和width相同。
当我在控制台中检查dimensionPopup1时,我会看到所有元素及其值。
我做错了什么?返回一个元素数组,因此必须访问数组中的各个元素才能获得它们的属性
例如
var items=document.getElementsByTagName(“div”);
调用(items,x=>console.log(x.clientHeight))代码>
A
B
B
C
C
C
getElementsByTagName
返回一个数组,因此您需要访问单个项。。。您不会从右侧的阵列中获得类似于offsetWidth
的内容。谢谢,我想我明白了。