Element: hasPointerCapture() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2020.

Thuộc tính hasPointerCapture() method of the Element interface checks whether the element on which it is invoked has pointer capture for the pointer identified by the given pointer ID.

Cú pháp

js
hasPointerCapture(pointerId)

Tham số

pointerId

The pointerId of a PointerEvent object.

Giá trị trả về

Một boolean value — true if the element does have pointer capture for the pointer identified by the given pointer ID, false if it doesn't.

Ví dụ

html
<div id="target">Touch this element with a pointer.</div>
js
const el = document.getElementById("target");
el.addEventListener("pointerdown", (ev) => {
  // Element 'target' will receive/capture further events
  el.setPointerCapture(ev.pointerId);

  // …

  // Check whether element still has pointer capture
  const pointerCap = el.hasPointerCapture(ev.pointerId);
  if (pointerCap) {
    // We've still got pointer capture
  } else {
    // oops, we've lost pointer capture!
  }
});

Đặc tả kỹ thuật

Specification
Pointer Events
# dom-element-haspointercapture

Trình duyệt hỗ trợ

Xem thêm