DOMTokenList.contains()

翻译不完整。 请帮助我们翻译这篇文章!

DOMTokenList接口的contains()方法返回Boolean类型。若传入的参数token包含在列表中时则返回true ,否则返回 false

语法

tokenList.contains(token);

参数

token
DOMString类型,用于判断是否存在于列表中的标记。

返回值

Boolean类型,当token包含在列表中时返回true,否则返回false

例子

在下面的列子中,我们通过Element.classList方法从 <span> 元素中获取DOMTokenList 对象。 然后测试验证"c" 是否包含在列表中, 结果输出到 <span>标签的内容 Node.textContent

HTML:

<span class="a b c"></span>

JavaScript:

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 Status Comment
DOM
contains()
Living Standard Initial definition

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
containsChrome Full support 8Edge Full support 12Firefox Full support YesIE Full support 10Opera Full support YesSafari Full support 5.1WebView Android Full support 3Chrome Android Full support 18Firefox Android Full support YesOpera Android Full support YesSafari iOS Full support 5.1Samsung Internet Android Full support 1.0

Legend

Full support  
Full support