Vue获取元素位置 getBoundingClientRect is not a function

点击input获取该元素位置

一开始使用 ref 获取dom元素,然后调用getBoundingClientRect()方法

<div @click="getInput" >
  <Input type="text"  readonly="readonly" ref="inputDom"></Input>
</div>

methods: {
   getInput () {
      let obj = this.$refs['inputDom']
      let objSet = obj.getBoundingClientRect()
      console.log(objSet)
   }
}

控制台会报错  Uncaught TypeError: obj.getBoundingClientRect is not a function

查看使用ref获取的dom元素即obj 是个VueComponent,改成下面的写法就ok了

<div @click="getInput" >
  <Input type="text"  readonly="readonly" ref="inputDom"></Input>
</div>

methods: {
   getInput: function (e) {
      let obj = e.target
      let objSet = obj.getBoundingClientRect()
      console.log(objSet)
   }
}

控制台输出结果 DOMRect {x: 296, y: 139, width: 206.25, height: 32, top: 139, …}

此时使用e.target获取的obj对象是 input.ivu-input.ivu-input-default

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值