Home > Web Front-end > Vue.js > How to get dom in vue.js

How to get dom in vue.js

coldplay.xixi
Release: 2020-11-10 11:02:57
Original
2757 people have browsed it

Vue.js method to obtain DOM: 1. Define the ref attribute in the original tag pair or sub-component in HTML, and use [this.$refs] after the [mounted(){}] method to obtain the DOM element ;2. Mounted continues to use [this.$refs] after modifying the content of the component.

How to get dom in vue.js

【Related article recommendations: vue.js

vue.js gets dom Method:

  • Define the ref attribute in the original tag pair or subcomponent in HTML, and use this.$refs after the mounted(){} method or this method. Specifically The ref value is used to obtain the DOM element. Because when using the previous hook function of mounted, the component has not been mounted on the DOM, so naturally the elements on the DOM cannot be obtained through $refs;

  • What needs to be distinguished is that printing this .$refs. The specific ref value. If it is an original tag pair, the output result is the original tag pair. If the ref attribute is in a sub-component tag, the output is the component object, not the content in the component's corresponding template;

  • this.$refs outputs a collection of tags or subcomponents that define the ref attribute contained in the current component;

  • During the component rendering process , for example, after modifying the content of the component during mounting, continue to use this.$refs. The ref corresponding to the modified component. At this time, what is obtained is the DOM element before modification. In order to obtain the modified DOM element , you must use the this.$nextTick() method, and use this.$refs in the callback function of this method. At this time, the modified DOM element is obtained. Of course, you can get the modified DOM elements under the updated function, but in some specific scenarios you need to get the modified DOM elements in mounted;

<!DOCTYPE html>
<html>
<head>
<meta charset=&#39;utf-8&#39;>
<title>Vue组件中获取DOM元素的方式之$refs的使用</title>
</head>
<body>
<div id=&#39;app&#39;></div>
<script type=&#39;text/javascript&#39; src=&#39;node_modules/vue/dist/vue.js&#39;></script>
<script type=&#39;text/javascript&#39;>
Vue.component(&#39;Btn&#39;,{
template:`
<button>我是按钮</button>
`
})
let App = {
data:function() {
return {
isShow:false
}
},
template:`
<div>
<input type=&#39;text&#39; ref=&#39;input1&#39;/>
<input type=&#39;text&#39; ref=&#39;input2&#39; v-show=&#39;isShow&#39;/>
<Btn ref=&#39;btn1&#39;/>
</div>
`,
//对应输出结果为下面第一张图
// mounted:function() {
// console.log(this.$refs)
// console.log(this.$refs.input1)
// console.log(this.$refs.input2)
// console.log(this.$refs.btn1)
// }
mounted:function() {
// 修改ref=input2的v-show值,让其显示, 接着获取该DOM并让其获得焦点,但是没法获得焦点,这是因为mounted内无法获得更新DOM后的DOM元素,这个时候需要调用this.$nextTick方法,在其回掉函数中重新执行代码this.$refs.input2.focus()
this.isShow = true
// this.$refs.input2.focus()
this.$nextTick(function() {
this.$refs.input2.focus()
})
},
}
let  vm = new Vue({
el:&#39;#app&#39;,
data:function() {
return {
}
},
components:{
App
},
template:`
<App/>
`
})
</script>
</body>
</html>
Copy after login

corresponds to the annotated mounted The content in

How to get dom in vue.js

The use of this.$nextTick in mounted allows the updated DOM elements to be obtained in mounted. Let the updated DOM element get focus in this code

How to get dom in vue.js

Related free learning recommendations:javascript(Video)

The above is the detailed content of How to get dom 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