How to get dom node in 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.
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. 2. Example 2: There are multiple nodes named rightItem in 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(){}). 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. 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: 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! AI-powered app for creating realistic nude photos Online AI tool for removing clothes from photos. Undress images for free AI clothes remover Swap faces in any video effortlessly with our completely free AI face swap tool! Easy-to-use and free code editor Chinese version, very easy to use Powerful PHP integrated development environment Visual web development tools God-level code editing software (SublimeText3) Steps to launch UniApp project preview in WebStorm: Install UniApp Development Tools plugin Connect to device settings WebSocket launch preview Generally speaking, uni-app is better when complex native functions are needed; MUI is better when simple or highly customized interfaces are needed. In addition, uni-app has: 1. Vue.js/JavaScript support; 2. Rich native components/API; 3. Good ecosystem. The disadvantages are: 1. Performance issues; 2. Difficulty in customizing the interface. MUI has: 1. Material Design support; 2. High flexibility; 3. Extensive component/theme library. The disadvantages are: 1. CSS dependency; 2. Does not provide native components; 3. Small ecosystem. uniapp development requires the following foundations: front-end technology (HTML, CSS, JavaScript) mobile development knowledge (iOS and Android platforms) Node.js other foundations (version control tools, IDE, mobile development simulator or real machine debugging experience) UniApp has many conveniences as a cross-platform development framework, but its shortcomings are also obvious: performance is limited by the hybrid development mode, resulting in poor opening speed, page rendering, and interactive response. The ecosystem is imperfect and there are few components and libraries in specific fields, which limits creativity and the realization of complex functions. Compatibility issues on different platforms are prone to style differences and inconsistent API support. The security mechanism of WebView is different from native applications, which may reduce application security. Application releases and updates that support multiple platforms at the same time require multiple compilations and packages, increasing development and maintenance costs. When choosing between UniApp and native development, you should consider development cost, performance, user experience, and flexibility. The advantages of UniApp are cross-platform development, rapid iteration, easy learning and built-in plug-ins, while native development is superior in performance, stability, native experience and scalability. Weigh the pros and cons based on specific project needs. UniApp is suitable for beginners, and native development is suitable for complex applications that pursue high performance and seamless experience. UniApp is based on Vue.js, and Flutter is based on Dart. Both support cross-platform development. UniApp provides rich components and easy development, but its performance is limited by WebView; Flutter uses a native rendering engine, which has excellent performance but is more difficult to develop. UniApp has an active Chinese community, and Flutter has a large and global community. UniApp is suitable for scenarios with rapid development and low performance requirements; Flutter is suitable for complex applications with high customization and high performance. Recommended component library for uniapp to develop small programs: uni-ui: Officially produced by uni, it provides basic and business components. vant-weapp: Produced by Bytedance, with a simple and beautiful UI design. taro-ui: produced by JD.com and developed based on the Taro framework. fish-design: Produced by Baidu, using Material Design design style. naive-ui: Produced by Youzan, modern UI design, lightweight and easy to customize.uni.createSelectorQuery().selectAll(".leftItem").boundingClienRect(res=>{
this.leftItemTop=res.map(item=>item.top)
}).exec(()=>{
console.log(this.leftItemTop)
})
uni.createSelectorQuery().selectAll(".rightItem").fields({
size:true
},res=>{
this.rightItemHeight=res.map(item=>{item.height})
}).exec(()=>{
console.log(this.rightItemHeight)
})
<block v-for="(item,index) in domData">
<view class="domItem">{{item.title}}</view>
</block>
data(){
return{
domData:[], //用于储存从接口中获取的DOM数据
domItemWidth:[] //用于储存获取的DOM结构的宽度
}
}
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)
})
})
}
})
Hot AI Tools
Undresser.AI Undress
AI Clothes Remover
Undress AI Tool
Clothoff.io
Video Face Swap
Hot Article
Hot Tools
Notepad++7.3.1
SublimeText3 Chinese version
Zend Studio 13.0.1
Dreamweaver CS6
SublimeText3 Mac version
Hot Topics
1387
52
How to start preview of uniapp project developed by webstorm
Apr 08, 2024 pm 06:42 PM
Which one is better, uniapp or mui?
Apr 06, 2024 am 05:18 AM
What basics are needed to learn uniapp?
Apr 06, 2024 am 04:45 AM
What are the disadvantages of uniapp
Apr 06, 2024 am 04:06 AM
Which is better, uniapp or native development?
Apr 06, 2024 am 05:06 AM
What is the difference between uniapp and flutter
Apr 06, 2024 am 04:30 AM
What component library does uniapp use to develop small programs?
Apr 06, 2024 am 03:54 AM