

English
返回一个 SelectorQuery
对象实例。可以在这个实例上使用 select
等方法选择节点,并使用 boundingClientRect
等方法选择需要查询的信息。
元服务 |
---|
√ |
HarmonyOS |
---|
HBuilderX 4.23 |
Tips:
uni.createSelectorQuery()
需要在生命周期 mounted
后进行调用。selectorQuery.in
方法。Object to query node information
将选择器的选取范围更改为自定义组件 component
内,返回一个 SelectorQuery
对象实例。(初始时,选择器仅选取页面范围的节点,不会选取任何自定义组件中的节点)。
代码示例
选项式 API
组合式 API
const query = uni.createSelectorQuery().in(this);
query
.select("#id")
.boundingClientRect((data) => {
console.log("得到布局位置信息" + JSON.stringify(data));
})
.exec();
Notice
在当前页面下选择第一个匹配选择器 selector
的节点,返回一个 NodesRef
对象实例,可以用于获取节点信息。
selector description:
selector
类似于 CSS 的选择器,但仅支持下列语法。
#the-id
.a-class.another-class
.the-parent > .the-child
.the-ancestor .the-descendant
.the-ancestor >>> .the-descendant
(H5 暂不支持)#a-node, .some-other-nodes
在当前页面下选择匹配选择器 selector
的所有节点,返回一个 NodesRef
对象实例,可以用于获取节点信息。
选择显示区域,可用于获取显示区域的尺寸、滚动位置等信息,返回一个 NodesRef
对象实例。
执行所有的请求。请求结果按请求次序构成数组,在 callback 的第一个参数中返回。
Object used to obtain node information
Obtain information about the nodes. The first parameter is node related information configuration (required); The second parameter is the callback function of the method, and the parameter is the specified related node information.
object parameter description
Field name | Type | Defaults | Required | Instruction | Platform difference description |
---|---|---|---|---|---|
id | Boolean | false | 否 | 是否返回节点 id | |
dataset | Boolean | false | 否 | 是否返回节点 dataset | App、微信小程序、H5、小红书小程序 |
rect | Boolean | false | No | Whether to return the node layout location (left right top bottom ) | |
size | Boolean | false | No | Whether to return the node size (width height ) | |
scrollOffset | Boolean | false | No | Whether to return scrollLeft scrollTop of the node, and the node must be scroll-view or viewport | |
properties | Array<string> | [] | 否 | 指定属性名列表,返回节点对应属性名的当前属性值(只能获得组件文档中标注的常规属性值,id class style 和事件绑定的属性值不可获取) | 仅 App 和微信小程序支持 |
computedStyle | Array<string> | [] | 否 | 指定样式名列表,返回节点对应样式名的当前值 | 仅 App 和微信小程序支持、小红书小程序 |
context | Boolean | false | 否 | 是否返回节点对应的 Context 对象 | 仅 App 和微信小程序支持、小红书小程序 |
node | Boolean | false | 否 | 是否返回节点对应的 Node 实例,当前仅支持 canvas 实例的获取 | H5, 微信小程序, 抖音小程序, 快手小程序 |
添加节点的布局位置的查询请求。相对于显示区域,以像素为单位。其功能类似于 DOM 的 getBoundingClientRect
。返回 NodesRef
对应的 SelectorQuery
。
callback return parameter
属性 | 类型 | 说明 |
---|---|---|
id | String | 节点的 ID |
dataset | Object | 节点的 dataset |
left | Number | 节点的左边界坐标 |
right | Number | 节点的右边界坐标 |
top | Number | 节点的上边界坐标 |
bottom | Number | 节点的下边界坐标 |
width | Number | 节点的宽度 |
height | Number | 节点的高度 |
添加节点的滚动位置查询请求。以像素为单位。节点必须是 scroll-view
或者 viewport
。返回 NodesRef
对应的 SelectorQuery
。
callback return parameter
属性 | 类型 | 说明 |
---|---|---|
id | String | 节点的 ID |
dataset | Object | 节点的 dataset |
scrollLeft | Number | 节点的水平滚动位置 |
scrollTop | Number | 节点的竖直滚动位置 |
Add a query request for the Context object of the node. Support the access of VideoContext
, CanvasContext
, and MapContext
.
Platform difference description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 | 小红书小程序 |
---|---|---|---|---|---|---|---|---|---|
√ | HBuilderX 2.4.7+ | √ | x | x | x | √ | √ | √ | √ |
callback return parameter
属性 | 类型 | 说明 |
---|---|---|
context | Object | Context object corresponding to the node |
Get the Node
node instance. Currently supports fetching of Canvas
.
Platform difference description
App | H5 | 微信小程序 | 支付宝小程序 | 百度小程序 | 抖音小程序、飞书小程序 | QQ小程序 | 快手小程序 | 京东小程序 | 小红书小程序 |
---|---|---|---|---|---|---|---|---|---|
x | x | √ | x | x | x | x | √ | √ | x |
callback return parameter
属性 | 类型 | 说明 |
---|---|---|
node | Object | Node instance corresponding to the node |
Notice
canvas
canvas
needs to be set with type="webgl"
for normal useuni
.createSelectorQuery()
.selectViewport()
.scrollOffset((res) => {
})
.exec();
let view = uni.createSelectorQuery().in(this).select(".test");
view
.fields(
{
size: true,
scrollOffset: true,
},
(data) => {
console.log("得到节点信息" + JSON.stringify(data));
}
)
.exec();
view
.boundingClientRect((data) => {
console.log("得到布局位置信息" + JSON.stringify(data));
})
.exec();
注意
<template>
<view class="wrapper">
<view ref="box" class="box">
<text class="info">Width: {{size.width}}</text>
<text class="info">Height: {{size.height}}</text>
<text class="info">Top: {{size.top}}</text>
<text class="info">Bottom: {{size.bottom}}</text>
<text class="info">Left: {{size.left}}</text>
<text class="info">Right: {{size.right}}</text>
</view>
</view>
</template>
<script>
// 注意平台差异
// #ifdef APP-NVUE
const dom = weex.requireModule("dom");
// #endif
export default {
data() {
return {
size: {
width: 0,
height: 0,
top: 0,
bottom: 0,
left: 0,
right: 0,
},
};
},
onReady() {
setTimeout(() => {
const result = dom.getComponentRect(this.$refs.box, (option) => {
console.log("getComponentRect:", option);
this.size = option.size;
});
console.log("return value:", result);
console.log("viewport:", dom.getComponentRect("viewport"));
}, 100);
},
};
</script>
类型 |
---|
SelectorQuery |
将选择器的选取范围更改为自定义组件component内
HarmonyOS |
---|
- |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
component | any | 否 | - | - |
类型 |
---|
SelectorQuery |
在当前页面下选择第一个匹配选择器selector的节点
HarmonyOS |
---|
- |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
selector | string | 是 | - | - | - |
类型 |
---|
NodesRef |
添加节点的布局位置的查询请求,相对于显示区域,以像素为单位
HarmonyOS |
---|
- |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
callback | (result: any) => void | 否 | - | - |
类型 |
---|
SelectorQuery |
添加节点的滚动位置查询请求,以像素为单位
HarmonyOS |
---|
- |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
callback | (result: any) => void | 是 | - | - |
类型 |
---|
SelectorQuery |
获取节点的相关信息,需要获取的字段在fields中指定
HarmonyOS |
---|
- |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
fields | NodeField | 是 | - | - | - |
|:-|:-|:-|:-|:-:|:-| |id|boolean|否|-|-|是否返回节点 id| |dataset|boolean|否|-|-|是否返回节点 dataset| |rect|boolean|否|-|-|是否返回节点布局位置(left right top bottom)| |size|boolean|否|-|-|是否返回节点尺寸(width height)| |scrollOffset|boolean|否|-|-|是否返回节点的 scrollLeft scrollTop,节点必须是 scroll-view 或者 viewport| |properties|Array<string>|否|-|-|指定属性名列表,返回节点对应属性名的当前属性值(只能获得组件文档中标注的常规属性值,id class style 和事件绑定的属性值不可获取)| |computedStyle|Array<string>|否|-|-|指定样式名列表,返回节点对应样式名的当前值| |context|boolean|否|-| - | |||||
callback | (result: any) => void | 否 | - | - |
类型 |
---|
SelectorQuery |
添加节点的 Context 对象查询请求
HarmonyOS |
---|
- |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
callback | (result: any) => void | 是 | - | - |
类型 |
---|
SelectorQuery |
获取 Node 节点实例。目前支持 Canvas 的获取。 获取节点的相关信息,需要获取的字段在fields中指定
HarmonyOS |
---|
- |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
callback | (result: any) => void | 是 | - | - | - |
类型 |
---|
SelectorQuery |
在当前页面下选择匹配选择器selector的所有节点
HarmonyOS |
---|
- |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
selector | string | 是 | - | - | - |
类型 |
---|
NodesRef |
选择显示区域
HarmonyOS |
---|
- |
类型 |
---|
NodesRef |
执行所有的请求
HarmonyOS |
---|
- |
名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 |
---|---|---|---|---|---|
callback | (result: Array<any>) => void | 是 | - | - | - |
类型 | 必备 |
---|---|
NodesRef | 否 |