DOMTokenList:supports() 方法

Baseline 已广泛支持

此功能已成熟,可跨多种设备和浏览器版本工作。它自 ⁨2018 年 4 月⁩ 起已在所有浏览器中可用。

DOMTokenList 接口的 supports() 方法,如果给定的 token 存在于关联属性的支持列表之中,则返回 true。此方法旨在支持功能检测。

语法

js
supports(token)

参数

token

包含要查询的 token 的字符串。

返回值

一个布尔值,指示是否找到该 token。

示例

js
const iframe = document.getElementById("display");

if (iframe.sandbox.supports("an-upcoming-feature")) {
  // support code for mystery future feature
} else {
  // fallback code
}

if (iframe.sandbox.supports("allow-scripts")) {
  // instruct frame to run JavaScript
  //
  // (NOTE: This feature is well-supported; this is just an example!)
  //
}

规范

规范
DOM
# ref-for-dom-domtokenlist-supports①

浏览器兼容性