Home > Web Front-end > Vue.js > body text

How to get the value of a certain dom element in vue.js

coldplay.xixi
Release: 2020-11-30 16:52:56
Original
5198 people have browsed it

Vue.js method of obtaining the value of a certain dom element: 1. In the [vue.js1.0] version, you can obtain the dom element through [v-el]; 2. In [Vue.js2.0] ] version uses the ref attribute to identify an element.

How to get the value of a certain dom element in vue.js

The operating environment of this tutorial: windows10 system, vue2.5.2, this article is applicable to all brands of computers.

[Recommended related articles: vue.js]

vue.js method of obtaining the value of a DOM element:

1, vue.js1.0 version

You can get the dom element through: v-el

For example:

html code :

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

js code:

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

Result:

How to get the value of a certain dom element in vue.js

2, Vue.js2.0 version

Vue.js 2.0 uses ref to replace v-el and v-ref, and uses the ref attribute to identify an element. .

When we write it becomes

<input type="text" name="xxx" id="xxx" ref=&#39;sxx&#39; />
<button @click="ok()">确定</button>
Copy after login

It remains the same when called

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

Related free learning recommendations: JavaScript( video)

The above is the detailed content of How to get the value of a certain dom element in vue.js. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template