如何获取反应元素的宽度

2024-06-21

我正在尝试创建一个范围输入,在滑块拇指上方显示工具提示。

我在网上浏览了一些普通的 JS 示例,似乎我需要元素的宽度才能完成此任务。

所以我只是想知道如何获取元素宽度?

几乎相当于 JQuery 方法$(element).width()


    class MyComponent extends Component {
      constructor(props){
        super(props)
        this.myInput = React.createRef()
      }

      componentDidMount () {
        console.log(this.myInput.current.offsetWidth)
      }

      render () {
        return (
        // new way - as of [email protected] /cdn-cgi/l/email-protection
        <div ref={this.myInput}>some elem</div>
        // legacy way
        // <div ref={(ref) => this.myInput = ref}>some elem</div>
        )
      }
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取反应元素的宽度 的相关文章

随机推荐