Uniapp's method of obtaining dom nodes: 1. Get the first node matching the selector through the "let dom=query.select(selector)" method; 2. Use "letdoms=query.selectAll(selector)" " method to get all nodes.
The operating environment of this tutorial: windows7 system, uni-app2.5.1 version, thinkpad t480 computer.
Recommended (free): uni-app development tutorial
uni-app Obtain DOM node
[Reference official website: https://uniapp.dcloud.io/api/ui/nodes-info?id=selectorqueryexec】
1. How to obtain the SelectorQuery object instance:
let query=uni.createSelectorQuery();
Function: Return a SelectorQuery object Instance, this instance is used to query the information of DOM nodes.
Notes:
(1) This method needs to be called after the life cycle is mounted.
(2) nvue technology does not support this method.
2. How to get the DOM node:
1. Get the first node that matches the selector:
let dom=query.select(selector)
2. Get all the nodes that match the selector:
letdoms=query.selectAll(selector)
Both the above two methods return NodesRef object instance, which is used to obtain DOM node information.
3. How to get the information of DOM nodes: (take doms as an example)
1. Get the layout position information of DOM nodes:
doms.boundingClienRect(function(res){ //res:{left,top,right,bottom,width,height} }).exec(function(){ //上述布局位置信息获取成功后执行的回调函数 })
2. Get the information of DOM nodes Scroll position information:
doms.scrollOffset(function(){ //res:{scrollLeft,scrollTop} }).exec(function(){ //上述滚动位置信息获取成功后执行的回调函数 })
3. Get all information of DOM nodes:
doms.fields({ rect:true, //是否返回节点布局位置信息{left,top,right,bottom} size:true, //是否返回节点尺寸信息{width,height} scrollOffset:true //是否返回节点滚动信息{scrollLeft,scrollTop} },function(res){ //res 可以返回第一个参数对象中指定为true的相关信息 }).exec(function(){ //上述节点信息获取成功后执行的回调函数 })
4. Code examples
1. Example 1: There are multiple in For nodes whose class name is leftItem, how to obtain the distance from the top of these nodes and assign these distances to an array named leftItemTop that has been defined in the data area.
uni.createSelectorQuery().selectAll(".leftItem").boundingClienRect(res=>{ this.leftItemTop=res.map(item=>item.top) }).exec(()=>{ console.log(this.leftItemTop) })
2. Example 2: There are multiple nodes named rightItem in
uni.createSelectorQuery().selectAll(".rightItem").fields({ size:true },res=>{ this.rightItemHeight=res.map(item=>{item.height}) }).exec(()=>{ console.log(this.rightItemHeight) })
5. Asynchronous problems caused by data rendering DOM:
[Reference official website: https://cn.vuejs.org/v2/api/#vm-nextTick]
[Recommended reading: https://segmentfault.com/a/1190000012861862]
**Question:**The variable temp in a certain data area affects the rendering of the DOM structure, and it is specified that after the variable is updated Another operation needs to be performed until the DOM structure is re-rendered. How can these other operations ensure that these other operations occur only after the DOM structure is re-rendered?
Solution: These operations that require the DOM structure to be re-rendered must be written in the callback function in the format of this&nextTick(function(){}).
<block v-for="(item,index) in domData"> <view class="domItem">{{item.title}}</view> </block>
Let’s just say that the above structure is a structure driven by data domData. The variable domData needs to obtain the necessary data from the interface first and then render it into the DOM structure.
data(){ return{ domData:[], //用于储存从接口中获取的DOM数据 domItemWidth:[] //用于储存获取的DOM结构的宽度 } }
When the variable domData gets the data from the interface, it must also ensure that the DOM structure is successfully rendered before the width of these structures can be obtained. Therefore, subsequent operations must use this.nextTick(function(){ }), which is written inside the callback function of this.nextTick(function(){}).
The code for the above example is as follows:
uni.request({ url:"http://localhost:8080/......", data:{.....}, success:res=>{ this.domData=res.data; this.nextTick(()=>{//该格式保证了domData已经得到接口数据并成功渲染DOM结构 uni.createSelectorQuery().selectAll(".domItem").fields({ size:true },res=>{ this.domItemWidth=res.map(item=>item.width) }).exec(()=>{ console.log(this.domItemWidth) }) }) } })
The above is the detailed content of How to get dom node in uniapp. For more information, please follow other related articles on the PHP Chinese website!

The article discusses debugging strategies for mobile and web platforms, highlighting tools like Android Studio, Xcode, and Chrome DevTools, and techniques for consistent results across OS and performance optimization.

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

The article discusses end-to-end testing for UniApp applications across multiple platforms. It covers defining test scenarios, choosing tools like Appium and Cypress, setting up environments, writing and running tests, analyzing results, and integrat

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

The article discusses common performance anti-patterns in UniApp development, such as excessive global data use and inefficient data binding, and offers strategies to identify and mitigate these issues for better app performance.

The article discusses using profiling tools to identify and resolve performance bottlenecks in UniApp, focusing on setup, data analysis, and optimization.

The article discusses strategies for optimizing network requests in UniApp, focusing on reducing latency, implementing caching, and using monitoring tools to enhance application performance.

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool