温馨提示×

vue中怎么获取dom元素中的内容

vue
清风
1014
2021-03-15 19:40:18
栏目: 编程语言
Vue开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

vue中怎么获取dom元素中的内容

vue中获取dom元素中内容的方法:

vue中是通过给标签加ref属性,然后在js中利用ref去引用它,从而操作该dom元素。

示例:

<template> 

 <div> 

  <div id="box" ref="mybox"> 

   DEMO 

  </div> 

 </div> 

</template> 

  

<script> 

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

  } 

 } 

  

</script> 

  

<style scoped>

 #box { 

  width100px

  height100px

  line-height100px

  font-size20px

  text-align: center; 

  border1px solid black; 

  margin50px;  

  color: yellow; 

 } 

</style>

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:怎么在JQuery中获取DOM元素

0