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

How to get DOM elements and set attributes with vue

一个新手
一个新手Original
2017-09-28 09:37:372877browse

I thought of 2 methods here:

Method 1:

Add id directly to the corresponding element, and then

document.getElementById("id");

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"

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!

Statement:
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
Previous article:git submission methodNext article:git submission method