@drivy/dom-query

中文文档
1.0.6发布于 2022/01/12MITTypeScriptESM + CJS零依赖近 30 天下载 3199

Utilities to select dom elements and handle events

原始使用文档
文档目录7 节
  1. Install
  2. Usage
  3. API
  4. Get elements from the DOM
  5. Transform existing javascript elements to EnhancedHTMLEleme…
  6. Handle events
  7. Advanced features

Enhanced HTML elements with query/event methods

Utility functions to easily perform DOM element manipulation:

  • Element selection based on css query selectors. Chainable. NodeLists automatically converted as Arrays
  • Helpers to Attach/detach events. Event delegation supported

This library is provided as an ES module with Typescript declarations.

Install

Install with npm:

$ npm install @drivy/dom-query

Install with yarn:

$ yarn add @drivy/dom-query

Usage

import { query, queryAll } from "@drivy/dom-query";

queryAll(".home-header").map(header => console.log(header));
// => <h1 class="home-header"> Welcome </h1>
// => <h1 class="home-header"> Get started </h1>

query(".confirm-button").on("click", e => {
  confirm(e.target);
});

API

Get elements from the DOM

query

  • selects an element from the DOM with a css query selector.
  • returns null if no element matches the selector.
  • chainable. Chained method will be based on the last selected element context

queryStrict

  • like query method but will raise en exception if no element matches the selector.

queryAll

  • selects a list of elements from the DOM with a css query selector. NodeList results will be converted to Arrays so you can directly access Array methods (forEach, map, reduce, filter...)

Examples:

import { query, queryStrict, queryAll } from "@drivy/dom-query"
...
const myElement = query(".js_my-element")
const mySubElement = myElement.query(".js_my-sub-element")
const mySubElements = myElement.queryAll(".js_my-sub-elements")

queryAll(".js_my-container .js_my-children-to-select").map(enhancedElement => {
  doSomething(enhancedElement)
})

const myElement2 = queryStrict(".js_my-existing-element")
//code below won't be reached if the previous element was not found
doSomething(myElement2)

Transform existing javascript elements to EnhancedHTMLElements

You can transform an existing javascript Element/List of Elements to an EnhancedHTMLElement/EnhancedHTMLElementList with the following methods:

  • toEnhancedHTMLElement
  • toEnhancedHTMLElementList

You have to use those methods when input elements are out of your scope (function args, event.target ...)

import { toEnhancedHTMLElement, toEnhancedHTMLElementList } from "@drivy/dom-query"
...

const myElement = toEnhancedHTMLElement(myHTMLElement);
const myElements = toEnhancedHTMLElementList(myHTMLElements);

Handle events

You can attach/detach events with the following methods:

EnhancedHTMLElement.on

  • attaches event like native target.addEventListener.
  • returns a "detach" function. When called, will remove the event listener like target.removeEventListener.

EnhancedHTMLElementList.on

  • same parameters and behavior than EnhancedHTMLElement.on but the event will be attached on each element contained in the list.
  • returns a "detach" function that when called will detach the events attached on each element of the list

EnhancedHTMLElement.onDelegate

  • does event delegation. pattern description here.
  • like .on method but takes an extra parameter (first one) as a css query selector to specify the children to use as event targets
  • returns a "detach" function. When called, will remove the event listener attached to the container.

EnhancedHTMLElementList.onDelegate

  • same parameters and behavior than EnhancedHTMLElement.onDelegate but the event delegation will be done for each element of the list.
  • returns a "detach" function that when called will detach the events attached on each element of the list

Examples:

// EnhancedHTMLElement

const detach = myElement.on("click", e => {
  doSomething(e.target)
})
...
detach() //same behavior than native target.removeEventListener

const detachDelegation = myContainer.onDelegate(".js_sub-elements", "click", e => {
  doSomething(e.target)
});
// clicking on a child having the "js_sub-elements" class will trigger the event

...
detachDelegation() // remove event listener used to delegate

// EnhancedHTMLElementList

const detach = queryAll(".js_my-elements").on("click", e => {
  doSomething(e.target)
})
...
detach()

const detachDelegations = queryAll(".js_my-elements").onDelegate(
  ".js_my-sub-elements",
  "click",
  e => {
    doSomething(e.target)
  }
)
...
detachDelegations()

Advanced features

Event configuration

When attaching an event with .on or .onDelegate you can specify an optional event configuration object as last parameter. This configuration object has a once boolean property that when true will automatically detach the event after having called once.

myElement2.on(
  "click",
  e => {
    doSomething(e.target);
    // event listener will be detached
  },
  {
    once: true
  }
);

Specializing the HTMLElement (ex: HTMLInputElement)

Because EnhancedHTMLElement enhances HTMLElement by default, to access properties/methods from an enhanced HTMLInputElement with Typescript (ex: myEnhancedInput.value), you must declare the target Type when calling the query/converting method

import { query, queryAll, toEnhancedHTMLElement } from "@drivy/dom-query"
...

const myEnhancedInput = query<HTMLInputElement>(".js_my-target-input");
doSomething(myEnhancedInput.value)

const myEnhancedInput2 = toEnhancedHTMLElement<HTMLInputElement>(myExistingInput);
doSomething(myEnhancedInput2.value)

const myEnhancedInputs = queryAll<HTMLInputElement>(".js_my-target-inputs");
myEnhancedInputs.forEach(myInput => {
  doSomething(myInput.value);
})

版本与发布记录

@drivy/dom-query 共发布 6 个版本,首次发布于 2019/06/26;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.6latest2022/01/124 年前
1.0.52020/08/316 年前
1.0.42019/12/036 年前
1.0.32019/10/177 年前
1.0.22019/10/167 年前
1.0.12019/06/267 年前

关于 @drivy/dom-query 的常见问题

@drivy/dom-query 是什么?

Utilities to select dom elements and handle events

如何安装 @drivy/dom-query?

在项目目录运行 npm install @drivy/dom-query,或使用 pnpm add / yarn add / bun add。

@drivy/dom-query 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@drivy/dom-query 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@drivy/dom-query 可以商用吗?

@drivy/dom-query 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@drivy/dom-query 的下载量有多少?

根据 npm 官方下载统计,@drivy/dom-query 在 2026/10/13 – 2026/11/11 这 30 天内被下载 3,199 次,日均约 107 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@drivy/dom-query 还在维护吗?

最新版本 1.0.6 发布于 2022/01/12(4 年前);至今共发布 6 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/06/26。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。