vue如何获取自定义元素属性参数值

偶尔还是会陷入到DOM操作上面去,其实你应该关心的是数据传递,而不是操作DOM。如果你是想获取data-num的数据,可以这样写:

55

getData:function (e,num) {
console.log(num)
}

这样就可以取到这个值了,如果你是确实想操作DOM,那你可以这样写:

55

getData:function (e) {
console.log(this.$refs.dataNum.dataset.num);
}

2.通过e.target.getAttribute

55

new Vue({
el:'#app',
methods:{
getData:function (e) {
console.log(e.target.getAttribute('data-num'))
}
}
})
3.自定义命名

如:html        :

js:

$('.live-dd').on("click",function(){var num = this.getAttribute('numId');location.href='/live/info?id='+num;})

使用jq的方法

var live_id = $('.vk_cq5').attr('classid')

    ref的用法  相当于操作dom
html : 

  
       
      DEMO  
    

  
  

js:
export default {  
  data () {  
    return {  
        
    }  
  },  
  mounted () {  
    this.init();  
  },  
  methods:{  
    init() {  
      const self = this;  
      this.$refs.mybox.style.color = 'red';  
      setTimeout(() => {  
        self.$refs.mybox.style.color = 'blue';  
      },2000)  
    }  
  }  
  
}  
---------------------
原文:https://blog.csdn.net/qq_41854017/article/details/79653096


本文固定链接: http://blog.wwllcchf.com/?p=651 | 吴文龙的个人博客

作者:wuwenlong 于2018年12月04日发表 &
vue如何获取自定义元素属性参数值 | 吴文龙的个人博客

报歉!评论已关闭.