search
HomeWeb Front-enduni-appHow to get dom node in uniapp

How to get dom node in uniapp

Dec 09, 2020 am 10:40 AM
uniapp

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.

How to get dom node in uniapp

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

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 . How to get the height of these nodes and assign these heights to a node that has been defined in the data area? An array named rightItem.

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!

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
How do you debug issues on different platforms (e.g., mobile, web)?How do you debug issues on different platforms (e.g., mobile, web)?Mar 27, 2025 pm 05:07 PM

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.

What debugging tools are available for UniApp development?What debugging tools are available for UniApp development?Mar 27, 2025 pm 05:05 PM

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

How do you perform end-to-end testing for UniApp applications?How do you perform end-to-end testing for UniApp applications?Mar 27, 2025 pm 05:04 PM

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

What are the different types of testing that you can perform in a UniApp application?What are the different types of testing that you can perform in a UniApp application?Mar 27, 2025 pm 04:59 PM

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

What are some common performance anti-patterns in UniApp?What are some common performance anti-patterns in UniApp?Mar 27, 2025 pm 04:58 PM

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.

How can you use profiling tools to identify performance bottlenecks in UniApp?How can you use profiling tools to identify performance bottlenecks in UniApp?Mar 27, 2025 pm 04:57 PM

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

How can you optimize network requests in UniApp?How can you optimize network requests in UniApp?Mar 27, 2025 pm 04:52 PM

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

How can you optimize images for web performance in UniApp?How can you optimize images for web performance in UniApp?Mar 27, 2025 pm 04:50 PM

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

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SecLists

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

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool