vue.js獲取某個dom元素的值


vue.js1.0版本

可以通過:v-el 來取得dom元素

例如:

html代碼:

<input type="text" name="xxx" id="xxx" v-el:sxx />
            <button @click="ok()">確定</button>

js代碼:

var vm = new Vue({
    el: '#app',
    methods: {
        ok() {
           var xx = this.$els.sxx.value;
           console.log(xx);
        }
    }
});

結果:

 

Vue.js2.0

Vue.js 2.0使用了 ref 替換掉了 v-el和v-ref,使用 ref 屬性來標識一個元素。 .

我們寫的時候就變成了

<input type="text" name="xxx" id="xxx" ref='sxx' />
<button @click="ok()">確定</button>

調用時還是一樣的

var vm = new Vue({
    el: '#app',
    methods: {
        ok() {
           var xx = this.$refs.sxx.value;
           console.log(xx);
        }
    }
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM