blob: 009c6c898d8d942b5c16532293e3f5cc04c997a2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
---
title: Document.createAttribute()
slug: Web/API/Document/createAttribute
tags:
- API
- DOM
- Méthode
- Referenz
translation_of: Web/API/Document/createAttribute
---
<div>{{ ApiRef("DOM") }}</div>
<p><strong>createAttribute</strong> erstellt einen neuen Attributsknoten und gibt ihn zurück.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox"><em>attribute</em> = document.createAttribute(name)
</pre>
<h3 id="Parameters" name="Parameters">Parameter</h3>
<ul>
<li><code>attribute</code> ist ein Attributsknoten.</li>
<li><code>name</code> ist ein String, der den Namen des Attributs enthält.</li>
</ul>
<h2 id="Example" name="Example">Beispiel</h2>
<pre class="brush:js"><html>
<head>
<title> create/set/get Attribut Beispiel</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 ohne die Verwendung von 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>
</pre>
<h2 id="Notes" name="Notes">Bemerkungen</h2>
<p>Der Rückgabewert ist ein Knoten des Typs <code>attribute</code>. Sobald man diesen wie im vorangegangenen Beispiel hat, kann man ihren Wert festlegen, indem man der <code>nodeValue</code> <em>property</em> einen String zuweist, oder in der alternativen Form durch Benutzung der <a href="/en-US/docs/DOM/element.setAttribute" title="DOM/element.setAttribute">setAttribute()</a> Methode. Der DOM beschränkt auf diese Art nicht, welche Arten von Attributen an das jeweilige Element zugewiesen werden dürfen.</p>
<h2 id="Specification" name="Specification">Spezifikation</h2>
<ul>
<li><a href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-1084891198">createAttribute </a></li>
</ul>
<h2 id="Siehe_auch">Siehe auch</h2>
<ul>
<li>{{domxref("document.createElement")}}</li>
</ul>
|