title: SelectorQuery

sidebar_label: SelectorQuery

The object for querying the node information

Reference

Methods

exec

Executes all requests. The request results form an array in the order of the requests and are returned in the first callback parameter.

Reference

  1. (callback?: (...args: any[]) => any) => NodesRef
Property Type Description
callback (…args: any[]) => any Callback function

API Support

API WeChat Mini-Program H5 React Native
SelectorQuery.exec ✔️ ✔️

select

Selects the first node in the current page that matches the selector selector. A NodesRef object instance is returned to obtain the node information.

selector Syntax

The selector is similar to the CSS selector, but only supports the following syntax.

  • ID selector:#the-id
  • class selector (more than one class selector can be specified in succession): .a-class.another-class
  • Child selector: .the-parent > .the-child
  • Descendant selector: .the-ancestor .the-descendant
  • Descendant selector across custom components: .the-ancestor >>> .the-descendant
  • Union of multiple selectors: #a-node, .some-other-nodes

Reference

  1. (selector: string) => NodesRef
Property Type Description
selector string Selector

Sample Code

  1. Taro.createSelectorQuery().select('#the-id').fields({
  2. dataset: true,
  3. size: true,
  4. scrollOffset: true,
  5. properties: ['scrollX', 'scrollY']
  6. }, function (res){
  7. res.dataset
  8. res.width
  9. res.height
  10. res.scrollLeft
  11. res.scrollTop
  12. res.scrollX
  13. res.scrollY
  14. }).exec()

API Support

API WeChat Mini-Program H5 React Native
SelectorQuery.select ✔️ ✔️

selectAll

Selects all nodes that match the selector “selector” in the current page.

selector Syntax

The selector is similar to the CSS selector, but only supports the following syntax.

  • ID selector:#the-id
  • class selector (more than one class selector can be specified in succession): .a-class.another-class
  • Child selector: .the-parent > .the-child
  • Descendant selector: .the-ancestor .the-descendant
  • Descendant selector across custom components: .the-ancestor >>> .the-descendant
  • Union of multiple selectors: #a-node, .some-other-nodes

Reference

  1. (selector: string) => NodesRef
Property Type Description
selector string Selector

API Support

API WeChat Mini-Program H5 React Native
SelectorQuery.selectAll ✔️ ✔️

selectViewport

Selects the display area. It can be used to get the display area size, scroll position, etc.

Reference

  1. () => NodesRef

Sample Code

  1. Taro.createSelectorQuery().selectViewport().scrollOffset(function (res) {
  2. res.id
  3. res.dataset
  4. res.scrollLeft
  5. res.scrollTop
  6. }).exec()

API Support

API WeChat Mini-Program H5 React Native
SelectorQuery.selectViewport ✔️ ✔️

in

Changes the selector’s selection range to the nodes within the custom component component. (Initially, the selector only selects nodes within the page and does not select any nodes in the custom component.)

Reference

  1. (component: Record<string, any>) => SelectorQuery
Property Type Description
component Record<string, any> Custom component instance

Sample Code

  1. Component({
  2. queryMultipleNodes () {
  3. const query = Taro.createSelectorQuery().in(this)
  4. query.select('#the-id').boundingClientRect(function(res){
  5. res.top // The upper boundary coordinate of the #the-id node within this component
  6. }).exec()
  7. }
  8. })

API Support

API WeChat Mini-Program H5 React Native
SelectorQuery.in ✔️ ✔️

API Support

API WeChat Mini-Program H5 React Native
SelectorQuery.exec ✔️ ✔️
SelectorQuery.select ✔️ ✔️
SelectorQuery.selectAll ✔️ ✔️
SelectorQuery.selectViewport ✔️ ✔️
SelectorQuery.in ✔️ ✔️