ref指令

1、案例1

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>ref指令</title>
</head>

<body>
    <div id="app">
        <button @click="showText">按钮</button>
        <p ref="hello">{{msg}}</p>
    </div>
    <script src="../js/vue.js" type="text/javascript"></script>
    <script>
        const vm = new Vue({
            el: "#app",
            data: {
                msg: "你好吗"
            },
            methods: {
                showText() {
                    //this.$refs.hello 获取p标签
                    //alert p标签的内容
                    alert(this.$refs.hello.textContent);
                }
            }
        });
    </script>
</body>

</html>

 

posted @ 2020-03-14 13:28  牛牛的自留地  阅读(577)  评论(0编辑  收藏  举报