SelectorQuery: select() method
在 SelectorQuery
指定的根节点的后代中选择第一个匹配指定选择器的节点。
语法
select(selector: string): NodesRef;
参数
selector
用于匹配节点的 CSS 选择器组合。第一个匹配指定选择器组合的节点将会返回。
用于匹配节点的 CSS 选择器必须满足下列语法:
选择器
// '#my-id'
<view id="my-id" />
// '.class1'
<view className="class1" />
- Attribute 选择器(注意自定义组件的
props
不属于 attribute
,因此无法使用 attribute
选择器匹配自定义组件的 props
)
[attribute]
:表示带有以 attr 命名的属性的元件。
[attribute=value]
:表示带有以 attr 命名的属性,且属性值为 value 的元件。
[attribute*=value]
:表示带有以 attr 命名的属性,且属性值至少包含一个 value 值的元件。
[attribute^=value]
:表示带有以 attr 命名的属性,且属性值是以 value 开头的元件。
[attribute$=value]
:表示带有以 attr 命名的属性,且属性值是以 value 结尾的元件。
// '[src]'
// '[src=xxx]'
// '[data-x]'
// '[data-x=xxx]'
<image src="xxx" data-x="xxx" />
// 'image#my-id[src=xxx].class1.class2'
<image id="my-id" className="class1 class2" src="xxx" data-x="xxx" />
组合器
// '.the-parent > .the-child'
<view className="the-parent">
<view className="the-child" />
</view>
// GOOD: '.the-ancestor .the-descendant'
// BAD: '.the-ancestor > .the-descendant'
<view className="the-ancestor">
<view>
<view className="the-descendant" />
</view>
</view>
返回值
代表该查询结果的 NodesRef
对象实例。
注意此时 SelectorQuery
只会将用户提供的查询参数保存在 NodesRef
对象中,并不会立即执行查询。
因此,通过以下方式查看节点信息或判断节点是否能够找到是不可行的:
let nodesRef = lynx.createSelectorQuery().select('#the-id');
console.log(nodesRef); // always returning a valid NodesRef object
注意事项