Element.removeAttribute()

您正在阅读此内容的英文版本,因为该语系尚未翻译。 帮助我们翻译此文章吧!

元素方法 removeAttribute() 从指定的元素中删除一个属性。

语法

element.removeAttribute(attrName);

参数

attrName
A DOMString specifying the name of the attribute to remove from the element. If the specified attribute does not exist, removeAttribute() returns without generating an error.

返回值

IE 返回boolean类型值,其他返回undefined

注意:因为 removeAttribute() 不会返回任何有效值,你不能使用链式方法(连续使用方法,例如 document.body.removeAttribute("first").removeAttribute("second")…)连续移除多个属性。

使用说明

若要彻底移除一个属性的效果,应当使用  removeAttribute(),而不是使用 setAttribute() 将属性值设置为  null。对于许多属性,如果仅将其值设为 null,这不会造达成和预期一样的效果。

DOM methods dealing with element's attributes:

Not namespace-aware, most commonly used methods Namespace-aware variants (DOM Level 2) DOM Level 1 methods for dealing with Attr nodes directly (seldom used) DOM Level 2 namespace-aware methods for dealing with Attr nodes directly (seldom used)
setAttribute (DOM 1) setAttributeNS setAttributeNode setAttributeNodeNS
getAttribute (DOM 1) getAttributeNS getAttributeNode getAttributeNodeNS
hasAttribute (DOM 2) hasAttributeNS - -
removeAttribute (DOM 1) removeAttributeNS removeAttributeNode -

例子

// Given: <div id="div1" align="left" width="200px"> 
document.getElementById("div1").removeAttribute("align"); 
// Now: <div id="div1" width="200px">

规范

DOM Level 2 Core: removeAttribute (introduced in DOM Level 1 Core)

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
removeAttributeChrome Full support YesEdge Full support 12
Notes
Full support 12
Notes
Notes This function doesn't respect boolean attributes' default values. See bug 12087679.
Firefox Full support YesIE ? Opera Full support YesSafari Full support 6WebView Android Full support YesChrome Android Full support YesFirefox Android Full support YesOpera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.