Obtain and manipulate dom elements in vue

Get the dom element can be used

elementList = document.querySelectorAll(selectors);//获取多个dom元素 如ul中的li
element = document.querySelector(selectors)//获取dom元素中的第一个元素

Use in vue

mounted(){//这里必须是mouted钩子
this.title = document.querySelector('#footer-box-title');
this.title.style.color = "#ff0000";
}

It should be used in mounted, because only when mounted is executed, vue has rendered the dom node. At this time, the dom node can be obtained. Try not to manipulate the dom element in vue, and use the ref operation attribute to obtain

<button ref="btn">获取ref</button>

this.$refs.btn.style.backgroundColor="#ff0000"

 

Guess you like

Origin blog.csdn.net/qq_35775675/article/details/80850553