아직 자원 봉사자들이 한국어로 현재 문서를 번역하지 않았습니다. 가입해서 이 문서가 번역되는 일에 함께 해 주세요!
createAttribute creates a new attribute node, and returns it.
Syntax
attribute = document.createAttribute(name)
Parameters
attributeis an attribute node.nameis a string containing the name of the attribute.
Example
<html>
<head>
<title> create/set/get Attribute example</title>
<script type="text/javascript">
function doAttrib() {
var node = document.getElementById("div1");
var a = document.createAttribute("my_attrib");
a.value = "newVal";
node.setAttributeNode(a);
alert(node.getAttribute("my_attrib")); // "newVal"
}
// alternative form not actually using createAttribute
//function doAttrib() {
// var node = document.getElementById("div1");
// node.setAttribute("my_attrib", "newVal");
// alert(node.getAttribute("my_attrib")); // "newVal"
//}
</script>
</head>
<body onload="doAttrib();">
<div id="div1">
<p>Some content here</p>
</div>
</body>
</html>
Notes
The return value is a node of type attribute. Once you have this node you can, as in the foregoing example, set its value by assigning a string to the nodeValue property, or in the alternate form by using the setAttribute() method. The DOM does not enforce what sort of attributes can be added to a particular element in this manner.