SelectorQuery.select
收藏
我的收藏

在当前页面或自定义组件中选择第一个匹配选择器的节点,返回一个 NodesRef 对象实例,可以用于获取节点信息。

语法

selectorQuery.select(selector)

参数说明

string 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

返回值

NodesRef

扫码体验

请使用字节宿主APP扫码

代码示例

开发者工具中预览

Bug & Tip

  • Tip:在使用 SelectorQuery.select 时建议使用后代选择器,不建议使用子选择器。例如:<scroll-view> 组件上使用子选择器后会选中 <scroll-view> 组件内部实现的节点,并不是预期节点,使用后代选择器可以避免该问题。