DOMTokenList.add()

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

语法

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

参数

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

Legend

Full support  
Full support