HTML DOM createAttribute() 方法

Document 对象 参考手册 Document 对象

实例

创建 class 属性, 属性值为 "democlass", 并将clas属性添加到 H1 元素中:

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

元素添加属性前:

Hello World

元素添加属性后:

Hello World


尝试一下 »

定义和用法

createAttribute()方法用于创建一个指定名称的属性,并返回Attr 对象属性。


浏览器支持

Internet ExplorerFirefoxOperaGoogle ChromeSafari

所有主要浏览器都支持 createAttribute() 方法


语法

document.createAttribute(attributename)

参数

参数类型描述
attributenameAttr object必须。要创建的属性名称。

返回值

类型描述
节点对象创建的属性

技术细节

DOM 版本Core Level 1 Document 对象


Document 对象 参考手册 Document 对象

0 个评论

要回复文章请先登录注册