For AI agents: the complete documentation index is available at /next/zh/llms.txt, the full documentation bundle is available at /next/zh/llms-full.txt, and this page is available as Markdown at /next/zh/api/lynx-api/nodes-ref/nodes-ref-fields.md.
Lynx
  • 简体中文
  • NodesRef: fields() method

    查询选定节点的属性。

    语法

    fields(fields: Record<string, boolean>, callback: (data: object, status: object) => void): SelectorQuery;

    参数

    fields

    一个 Record<string, boolean>,描述是否需要查询对应类型的属性。可查询的属性类型取值范围如下所示,不在下列范围内的取值将被忽略:

    • attribute(可选)

      • 节点的 attribute
        • 只会返回自定义属性,idclassstyledataset 等属性不会返回。
        • 值为 null / undefined / function 的属性不会返回。
        • 返回值的 key 是 kebab case。
    • class(可选)

    • dataset(可选)

    • id(可选)

    • index(可选)

    • query(可选)

      • 以该节点为根节点的 SelectorQuery 对象
        • 在该 SelectorQuery 对象上执行的查询,范围被限定为该节点及其后代节点。
        • 在该 SelectorQuery 对象上调用 selectRoot() 将返回代表该节点的 NodesRef 对象。
    • tag(可选)

    • unique_id(可选)

    callback

    一个回调函数,查询结果将作为回调函数的参数返回。回调函数有两个参数:

    第一个参数返回查询结果。

    • 如果该 NodesRef 表示一个节点,则返回包含查询结果的 Record<string, any> 对象。如果节点未找到,则返回 null
    • 如果该 NodesRef 表示多个节点,则返回一个 Record<string, any> 对象的数组,数组内的每一个元件对应一个节点的查询结果。如果节点均未找到,则返回空数组。

    第二个参数返回查询的状态(错误信息)。

    返回值

    包含了该任务的 SelectorQuery 对象。调用 exec() 来执行任务。

    示例

    lynx
      .createSelectorQuery()
      .select('#id')
      .fields(
        {
          id: true,
          dataset: true,
          tag: true,
        },
        (data, status) => {
          console.log(data, status);
        },
      )
      .exec();

    可能的输出:

    // data
    {"tag":"text","dataset":{"hello":"world"},"id":"my-id"}
    
    // status
    {"data":"success","code":0}

    兼容性

    LCD tables only load in the browser

    除非另有说明,本项目采用知识共享署名 4.0 国际许可协议进行许可,代码示例采用 Apache License 2.0 许可协议进行许可。