DOMTokenList.contains()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
DOMTokenList
接口的 contains()
方法返回 Boolean
类型。若传入的参数 token
包含在列表中时则返回true
,否则返回 false
。
语法
tokenList.contains(token);
参数
返回值
Boolean
类型,当token
包含在列表中时返回true
,否则返回false
。
示例
在下面的列子中,我们通过Element.classList
方法从 <span>
元素中获取DOMTokenList
对象。然后测试验证"c"
是否包含在列表中,结果输出到 <span>
标签的内容 Node.textContent
。
HTML:
html
<span class="a b c"></span>
JavaScript:
js
let span = document.querySelector("span");
let classes = span.classList;
let result = classes.contains("c");
if (result) {
span.textContent = "The classList contains 'c'";
} else {
span.textContent = "The classList does not contain 'c'";
}
结果如下:
规范
Specification |
---|
DOM Standard # ref-for-dom-domtokenlist-contains① |
Browser compatibility
BCD tables only load in the browser