DOMTokenList.add()

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

DOMTokenList 接口的 add() 方法将给定的标记添加到列表中。 

语法

tokenList.add(token1[, token2[, ...tokenN]]);

参数

tokenN
一个 DOMString,表示你要添加到列表里的标记。

返回值

undefined

例子

在下面的例子中,我们使用 Element.classList<span> 元素上设置的类列表检索为 DOMTokenList。然后,我们将新标记添加到列表中,并将列表写入 <span> 元素的Node.textContent

HTML:

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

JavaScript:

var span = document.querySelector("span");
var classes = span.classList;
classes.add("d");
span.textContent = classes;

结果如下:

你也可以添加多个标记:

span.classList.add("d", "e", "f");

规范

Specification Status Comment
DOM
add()
Living Standard Initial definition

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
addChrome Full support 1Edge Full support 12Firefox Full support YesIE Full support 10Opera Full support YesSafari Full support 5.1WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support 5.1Samsung Internet Android Full support Yes
Multiple arguments for add()Chrome Full support 24Edge Full support 12Firefox Full support 26IE No support NoOpera Full support 15Safari Full support 7WebView Android Full support ≤37Chrome Android Full support 25Firefox Android Full support 26Opera Android Full support 14Safari iOS Full support 7Samsung Internet Android Full support 1.5

Legend

Full support  
Full support
No support  
No support