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小程序。
第二部分:如何開通一個小商店