selectorQuery.in,选择第一个匹配选择器selector的节点
2020-09-27
导读:在当前页面下选择第一个匹配选择器 selector 的节点,返回一个 NodesRef 对象实例,可以用于获取节点信息。 selector 类似于CSS的选择器,但仅支持下列语法。 ID选择器: #the-id class选择器...
在当前页面下选择第一个匹配选择器selector
的节点,返回一个NodesRef
对象实例,可以用于获取节点信息。
selector
类似于CSS的选择器,但仅支持下列语法。
-
ID选择器:
#the-id
-
class选择器(可以连续指定多个):
.a-class.another-class
-
子元素选择器:
.the-parent > .the-child
-
后代选择器:
.the-ancestor .the-descendant
-
跨自定义组件的后代选择器:
.the-ancestor >>> .the-descendant
-
多选择器的并集:
#a-node, .some-other-nodes
selectorQuery.selectAll(selector)
在当前页面下选择匹配选择器selector
的节点,返回一个NodesRef
对象实例。 与selectorQuery.selectNode(selector)
不同的是,它选择所有匹配选择器的节点。
selectorQuery.selectViewport()
选择显示区域,可用于获取显示区域的尺寸、滚动位置等信息,返回一个NodesRef
对象实例。
nodesRef.boundingClientRect([callback])
添加节点的布局位置的查询请求,相对于显示区域,以像素为单位。其功能类似于DOM的getBoundingClientRect。返回值是nodesRef对应的selectorQuery。
返回的节点信息中,每个节点的位置用left
、right
、top
、bottom
、width
、height
字段描述。如果提供了callback回调函数,在执行selectQuery的exec方法后,节点信息会在callback中返回。
示例代码:
Page({
getRect: function(){
wx.createSelectorQuery().select('#the-id').boundingClientRect(function(rect){
rect.id // 节点的ID
rect.dataset // 节点的dataset
rect.left // 节点的左边界坐标
rect.right // 节点的右边界坐标
rect.top // 节点的上边界坐标
rect.bottom // 节点的下边界坐标
rect.width // 节点的宽度
rect.height // 节点的高度
}).exec()
},
getAllRects: function(){
wx.createSelectorQuery().selectAll('.a-class').boundingClientRect(function(rects){
rects.forEach(function(rect){
rect.id // 节点的ID
rect.dataset // 节点的dataset
rect.left // 节点的左边界坐标
rect.right // 节点的右边界坐标
rect.top // 节点的上边界坐标
rect.bottom // 节点的下边界坐标
rect.width // 节点的宽度
rect.height // 节点的高度
})
}).exec()
}
})
更多微信小程序开发教程,关注hi小程序。
第二部分:如何开通一个小商店