18-基础-ref 操作 DOM

<!DOCTYPE html>
<html>

<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>
</head>

<body>
    <div id='app'>
        <!--  ref操作dom
      1. 给要操作的元素设置ref属性 值随便写
      2. 在js中选项mounted(){获取要操作的dom元素}-->
        <input type="text" ref="txt">
        <p ref='ppp'>pppppppp</p>
        <a href="#" ref='aaa'>aaaaaaaa</a>
    </div>
    <script src='./vue.js'></script>
    <script>
        new Vue({
            el: '#app',
            data: {},
            methods: {},
            mounted() {
                console.log("mounted被触发----");
                this.$refs.txt.focus();
                console.log(this.$refs.txt);//input标签
                console.log(this.$refs.ppp); //p标签
                console.log(this.$refs.aaa);//a标签
                console.log('文本:' + this.$refs.aaa.innerText);
                //a标签的内容
            }
        });
    </script>
</body>

</html>

18-基础-ref 操作 DOM

上一篇:vue生命周期钩子函数的正确使用方式


下一篇:Vue获取DOM元素样式和样式更改