vue异步加载dom元素之后无法获得

vue存在大量的异步加载问题,比如动态创建dom元素,若你紧接着去获取创建的dom元素是获取不到的。

解决办法:

第一种办法比较low,使用setTimeout方法,让获取dom的代码在动态创建元素之后一段时间(这个时间非常的短)去执行。但这种方法应该是存在风险的,不推荐。

第二种办法 在将要执行的代码上套一层 this.$nextTick()

例如:

this.$nextTick(function() {
        let grids = _that.$refs.datamessage;
        console.log(grids);
        for (let i = 0; i < grids.length; i++) {
          let xb = grids[i].getAttribute("index");
          //alert(xb);
          if (_that.value.indexOf(xb) != -1) {
            grids[i].setAttribute("ifSelect", "true");
            grids[i].style.backgroundColor = "#b3d8ff";
            grids[i].style.color = "#409eff";
          }
        }
      });

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章