removeAttribute()方法從元素中刪除具有指定名稱的屬性。
注意:由于removeAttribute()不返回值,所以不能將多個調(diào)用鏈接在一起來一次性刪除多個屬性。
使用getAttribute()方法可返回元素的屬性值。
使用setAttribute()方法可添加新屬性或更改元素上現(xiàn)有屬性的值。
element.removeAttribute(attrName)
document.getElementsByTagName("H1")[0].removeAttribute("class");測試看看?/?所有瀏覽器完全支持removeAttribute()方法:
| Method | ![]() | ![]() | ![]() | ![]() | ![]() |
| removeAttribute() | 是 | 是 | 是 | 是 | 是 |
| 參數(shù) | 描述 |
|---|---|
| attrName | 字符串,表示要從元素中刪除的屬性的名稱 |
| 返回值: | Undefined |
|---|---|
| DOM版本: | DOM 2級 |
找出錨元素是否具有href屬性。如果存在href,使用removeAttribute刪除href屬性:
var a = document.getElementById("myLink");
if (a.hasAttribute("href")) {
a.removeAttribute("href");
}測試看看?/?HTML教程:HTML屬性
HTML DOM參考:getAttribute()方法
HTML DOM參考:setAttribute()方法
HTML DOM參考:hasAttribute()方法