aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/api/node/nodename/index.html
blob: b9c95b6e2f1821ad68c3f425c3743a282902a31c (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
---
title: Node.nodeName
slug: Web/API/Node/nodeName
translation_of: Web/API/Node/nodeName
---
<div>
<div>{{APIRef("DOM")}}</div>
</div>

<p><code><strong>Node.nodeName</strong></code> 唯讀屬性會回傳目前節點名稱的字串。</p>

<p>不同類型節點之回傳值:</p>

<table class="standard-table">
 <tbody>
  <tr>
   <th>介面</th>
   <th>nodeName 值</th>
  </tr>
  <tr>
   <td>{{domxref("Attr")}}</td>
   <td>The value of {{domxref("Attr.name")}}</td>
  </tr>
  <tr>
   <td>{{domxref("CDATASection")}}</td>
   <td><code>"#cdata-section"</code></td>
  </tr>
  <tr>
   <td>{{domxref("Comment")}}</td>
   <td><code>"#comment"</code></td>
  </tr>
  <tr>
   <td>{{domxref("Document")}}</td>
   <td><code>"#document"</code></td>
  </tr>
  <tr>
   <td>{{domxref("DocumentFragment")}}</td>
   <td><code>"#document-fragment"</code></td>
  </tr>
  <tr>
   <td>{{domxref("DocumentType")}}</td>
   <td>The value of {{domxref("DocumentType.name")}}</td>
  </tr>
  <tr>
   <td>{{domxref("Element")}}</td>
   <td>The value of {{domxref("Element.tagName")}}</td>
  </tr>
  <tr>
   <td>{{domxref("Entity")}}</td>
   <td>The entity name</td>
  </tr>
  <tr>
   <td>{{domxref("EntityReference")}}</td>
   <td>The name of entity reference</td>
  </tr>
  <tr>
   <td>{{domxref("Notation")}}</td>
   <td>The notation name</td>
  </tr>
  <tr>
   <td>{{domxref("ProcessingInstruction")}}</td>
   <td>The value of {{domxref("ProcessingInstruction.target")}}</td>
  </tr>
  <tr>
   <td>{{domxref("Text")}}</td>
   <td><code>"#text"</code></td>
  </tr>
 </tbody>
</table>

<h2 id="語法">語法</h2>

<pre class="syntaxbox">var <em>str</em> = <em>node</em>.nodeName;
</pre>

<h2 id="範例">範例</h2>

<p>Given the following markup:</p>

<pre class="brush:html">&lt;div id="d1"&gt;hello world&lt;/div&gt;
&lt;input type="text" id="t"/&gt;
</pre>

<p>and the following script:</p>

<pre class="brush:js">var div1 = document.getElementById("d1");
var text_field = document.getElementById("t");

text_field.value = div1.nodeName;
</pre>

<p>In XHTML (or any other XML format), <code>text_field</code>'s value would read "div". However, in HTML, <code>text_field</code>'s value would read "DIV", because <code>nodeName</code> and <code>tagName</code> return in upper case on HTML elements in DOMs flagged as HTML documents. Read more <a href="http://ejohn.org/blog/nodename-case-sensitivity/" title="http://ejohn.org/blog/nodename-case-sensitivity/">details on nodeName case sensitivity in different browsers</a>.</p>

<p>Note that <code><a href="/en-US/docs/DOM/element.tagName" title="DOM/element.tagName">tagName</a></code> property could have been used instead, since <code>nodeName</code> has the same value as <code>tagName</code> for an element. Bear in mind, however, that <code>nodeName</code> will return <code>#text</code> for text nodes while <code>tagName</code> will return <code>undefined</code>.</p>

<h2 id="規範">規範</h2>

<ul>
 <li><a href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-F68D095">DOM Level 2 Core: Node.nodeName</a></li>
 <li><a href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-F68D095">DOM Level 3 Core: Node.nodeName</a></li>
 <li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/dom.html#apis-in-html-documents">HTML 5: APIs in HTML documents</a></li>
</ul>