Latest web development tutorials

HTML DOM setAttributeNode() 方法

元素對象參考手冊 元素對象

實例

var atr=document.createAttribute("class");
atr.nodeValue="democlass";
document.getElementsByTagName("H1")[0].setAttributeNode(atr);

設置屬性節點之前:

Hello World

設置屬性節點之後:

Hello World


嘗試一下»

定義和用法

setAttributeNode() 方法用於添加新的屬性節點。

如果元素中已經存在指定名稱的屬性,那麼該屬性將被新屬性替代。 如果新屬性替代了已有的屬性,則返回被替代的屬性,否則返回NULL。


瀏覽器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要瀏覽器都支持setAttributeNode() 方法


語法

element .setAttributeNode( attributenode )

參數

参数 类型 描述
attributenode Attr 对象 必须。你要添加的属性节点。

返回值

类型 描述
Attr 对象 替换的属性节点, 如果没有则返回 null

技術細節

DOM 版本 Core Level 1 Element Object


元素對象參考手冊 元素對象