document.getElementsByClassName(‘...‘)获取不到元素

文章讨论了在Vue应用中,使用`getElementById`和`getElementsByClassName`获取DOM元素时,即使代码正确执行但输出可能为空的问题,原因可能是Vue的生命周期阶段问题或数据未及时更新,链接提供了相关解决方案的参考.
摘要由CSDN通过智能技术生成

代码:

let elementById = document.getElementById('aaa');
console.log(elementById.offsetLeft)
let elements1 = document.getElementsByClassName('gantt_menu');
console.log(elements1)

控制台:

更改代码:

let elementById = document.getElementById('aaa');
console.log(elementById.offsetLeft)
let elements1 = document.getElementsByClassName('gantt_menu');
console.log(elements1[0])

控制台:

 

css:

 <div class="gantt_group" v-for="item of taskInfo">
   <div class="gantt_menu">{{ item.name }}</div>
      <div class="gantt_body"
                   :style="{'left': mathPosition(itemBody.startDate),'width':mathGanttLength(itemBody.startDate,itemBody.endDate)}" v-for="itemBody of item.stage">
       {{ itemBody.text }}
    </div>
 </div>

看了很多资料是由于vue没有加载出来,导致为空,但我的已经打印出来了只要使用遍历或索引就是空 ,这是博客上的解答https://blog.csdn.net/weixin_41602509/article/details/86661758icon-default.png?t=N7T8https://blog.csdn.net/weixin_41602509/article/details/86661758

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值