Vue—ref获取DOM元素

  • Post author:
  • Post category:vue




获取p标签元素

当获取元素或者组件的DOM时候,用ref=”名称”定义,用his.$refs.名称 来调用

<p ref="getDom">{{msg}}</p>

----------------------------------------------------------------------------
 new Vue({
     el:'#app',
     data:{
         msg:'sdfdfdf'
     },
     methods: {
         getparament(){
console.log(this.$refs.getDom.innerText);
             

         }
         
     }
    });


案例代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
       <!--//导入js包-->
       <script src="js/vue.js"></script>
       <!-- Bootstrap -->
       <link href="./bootstrap/dist/css/bootstrap.css" rel="stylesheet">
       <script src="https://cdn.jsdelivr.net/npm/vue-resource@1.5.1"></script>
       <link href="./css/Animate.css" rel="stylesheet">
</head>
<body>
    <div id="app">
<p ref="getDom">{{msg}}</p>
<input type="button" value="得到DOM" @click="getparament">
    </div>
    <script>
     new Vue({
     el:'#app',
     data:{
         msg:'sdfdfdf'
     },
     methods: {
         getparament(){
console.log(this.$refs.getDom.innerText);
             

         }
         
     }
    });
    </script>
</body>
</html>



版权声明:本文为wulianlang6162原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。