可以使用以下代码来获取元素宽度:

class MyClass {
  constructor() {
    this.element = document.getElementById('myElement');
  }
  
  getWidth() {
    return this.element.offsetWidth;
  }
}

const myInstance = new MyClass();
console.log(myInstance.getWidth());

这里使用了 offsetWidth 属性来获取宽度。该属性返回元素的整体宽度,包括边框、内边距和滚动条(如果存在)。如果需要获取内容区域的宽度,可以使用 clientWidth 属性。

js使用类拿到宽度

原文地址: https://www.cveoy.top/t/topic/iNx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录