0

我尝试获取元素的宽度,但返回值错误

   let container = document.querySelector('.content')
    console.log(container.offsetWidth) //this will log wrong value 1182px instead of 960px

  console.log(getComputedStyle(container).width) //1182px anyway I don't have any idea from where the value comes

在此处输入图像描述

我不知道为什么返回的值是错误的

4

1 回答 1

0

尝试这个。这在获得偏移宽度之前对我有用,您可以将宽度设置为

let container = document.querySelector('.content')
container.style.width = "fit-content";
console.log(container.offsetWidth);
于 2019-01-06T09:30:09.463 回答