HTML DOM createAttribute() method
HTML DOM createAttribute() Method
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <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 Example»
Click the "Run Example" button to view the online example
Definition and usage
The createAttribute() method is used to create an attribute with a specified name and return the Attr object Attributes.
Browser support
All major browsers support the createAttribute() method
Syntax
document.createAttribute(attributename)
Parameters
Type | Description | |
---|---|---|
attributename | Attr objectRequired. The name of the property to create. |
Description | |
---|---|
Created Properties |
Core Level 1 Document Object |
---|