小白教程
所有教程
关于
Search
172.70.127.48
172.70.127.48
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Met document createattribute”的源代码
本页内容
上一节:
Prop_doc_cookie
下一节:
Met_document_createcomment
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:document.createAttribute()}}[[Category:dom obj document|9]] = HTML DOM createAttribute() 方法 = [[dom obj document|Document 对象]] 创建 class 属性, 属性值为 "democlass", 并将clas属性添加到 H1 元素中: <sample title="" desc="" lang="html" hererun="1"> var att=document.createAttribute("class"); att.value="democlass"; document.getElementsByTagName("H1")[0].setAttributeNode(att); </sample> 元素添加属性前: <sample title="" desc="" lang="html" hererun="1"> Hello World </sample> 元素添加属性后: <sample title="" desc="" lang="html" hererun="1"> Hello World </sample> <run name=""> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>小白教程(xiaobai.wang)</title> <style type="text/css"> .democlass{ color:red; } </style> </head> <body> <h1>Hello World</h1> <p id="demo">单击按钮来创建一个“类”属性值“democlass”插入到上面的H1元素。</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var h1=document.getElementsByTagName("H1")[0]; var att=document.createAttribute("class"); att.value="democlass"; h1.setAttributeNode(att); } </script> </body> </html> </run> == 定义和用法 == createAttribute()方法用于创建一个指定名称的属性,并返回Attr 对象属性。 == 浏览器支持 == 所有主要浏览器都支持 createAttribute() 方法 == 语法 == <pre> document.createAttribute(attributename)</pre> == 参数 == {| class="table table-striped table-hover" ! 参数 ! 类型 ! 描述 |- | attributename | Attr object | 必须。要创建的属性名称。 |} == 返回值 == {| class="table table-striped table-hover" ! 类型 ! 描述 |- | 节点对象 | 创建的属性 |} == 技术细节 == {| class="table table-striped table-hover" | DOM 版本 | Core Level 1 Document 对象 |} [[dom obj document|Document 对象]]
返回至“
Met document createattribute
”。
上一节:
Prop_doc_cookie
下一节:
Met_document_createcomment