vue.js 'document.getElementById' 简写 [英] vue.js 'document.getElementById' shorthand

查看:30
本文介绍了vue.js 'document.getElementById' 简写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

vue.js 是否像 JQuery 的 $('#id') 一样有 document.getElementById('#id') 的简写?

如果是这样,文档中对此的参考在哪里,以便我可以找到其他信息?

解决方案

vue 2 没有速记方法.

Jeff 的方法在 vue 2 中似乎已经被弃用了.

这是您实现目标的另一种方式.

 var app = new Vue({el:'#app',方法: {showMyDiv() {console.log(this.$refs.myDiv);}}});

<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></脚本><div id='应用程序'><div id="myDiv" ref="myDiv"></div><button v-on:click="showMyDiv" >显示我的 Div</button>

Does vue.js have a shorthand for document.getElementById('#id') like JQuery's $('#id')?

If so, where is the reference for this in the docs so I can locate other information?

解决方案

Theres no shorthand way in vue 2.

Jeff's method seems already deprecated in vue 2.

Heres another way u can achieve your goal.

 var app = new Vue({
    el:'#app',
    methods: {    
        showMyDiv() {
           console.log(this.$refs.myDiv);
        }
    }
    
   });

<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<div id='app'>
   <div id="myDiv" ref="myDiv"></div>
   <button v-on:click="showMyDiv" >Show My Div</button>
</div>

这篇关于vue.js 'document.getElementById' 简写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆