Home > Web Front-end > JS Tutorial > How to get DOM elements and set attributes with vue

How to get DOM elements and set attributes with vue

一个新手
Release: 2017-09-28 09:37:37
Original
2956 people have browsed it

I thought of 2 methods here:

Method 1:

Add id directly to the corresponding element, and then

document.getElementById("id");
Copy after login

get it, and then set the corresponding attribute or Style

Method 2:

Use ref, add ref="name" to the corresponding element and then use this.$refs.name to get the element

Note: Before obtaining the corresponding element, you must mount it in the mount hook, otherwise the obtained value will be empty,

If you add an ID to a sub-component and modify the custom attribute, the modified sub-component will be loaded directly On the corresponding outer p, the value of the original custom attribute of the subcomponent will not be changed

If you add ref to the subcomponent, and then change the value of the corresponding custom attribute after obtaining the DOM element, vue will report an error:


 Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "title"
Copy after login

The above is the detailed content of How to get DOM elements and set attributes with vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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