aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/node/nodename/index.html
blob: 632d06407f0b45624f78f73fb89d2f64858ff555 (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>
 {{APIRef}}</div>
<div>
  </div>
<div>
 <span style="font-size: 2.14285714285714rem; font-weight: 700; letter-spacing: -1px; line-height: 30px;">概述</span></div>
<p>返回当前节点的节点名称</p>
<h2 id="语法">语法</h2>
<pre class="syntaxbox">var <em>str</em> = <em>node</em>.nodeName;
</pre>
<ul>
 <li><code>str</code> 是一个存储了当前节点的节点名称的字符串.</li>
 <li><code>nodeName 是一个只读属性</code>.</li>
</ul>
<h2 id="附注">附注</h2>
<p>下表列出了所有类型的节点的<code>nodeName</code>属性的值.</p>
<table class="standard-table">
 <tbody>
  <tr>
   <th>接口</th>
   <th>nodeName属性值</th>
  </tr>
  <tr>
   <td><a href="/zh-CN/docs/DOM/Attr" title="DOM/Attr">Attr</a></td>
   <td>等同于 <code><a href="/zh-CN/docs/DOM/Attr.name" title="DOM/Attr.name">Attr.name</a></code> 属性的值</td>
  </tr>
  <tr>
   <td><a href="/zh-CN/docs/DOM/CDATASection" title="DOM/CDATASection">CDATASection</a></td>
   <td>"#cdata-section"</td>
  </tr>
  <tr>
   <td><a href="/zh-CN/docs/DOM/Comment" title="DOM/Comment">Comment</a></td>
   <td>"#comment"</td>
  </tr>
  <tr>
   <td><a href="/zh-CN/docs/DOM/document" title="DOM/document">Document</a></td>
   <td>"#document"</td>
  </tr>
  <tr>
   <td><a href="/zh-CN/docs/DOM/DocumentFragment" title="DOM/DocumentFragment">DocumentFragment</a></td>
   <td>"#document-fragment"</td>
  </tr>
  <tr>
   <td><a href="/zh-CN/docs/DOM/DocumentType" title="DOM/DocumentType">DocumentType</a></td>
   <td>
    <p>等同于 <code><a href="/zh-CN/docs/DOM/DocumentType.name" title="DOM/DocumentType.name">DocumentType.name</a></code><code> 属性的值</code></p>
   </td>
  </tr>
  <tr>
   <td><a href="/zh-CN/docs/DOM/element" title="DOM/element">Element</a></td>
   <td>
    <p>等同于 <code><a href="/zh-CN/docs/DOM/element.tagName" title="DOM/element.tagName">Element.tagName</a></code> 属性的值</p>
   </td>
  </tr>
  <tr>
   <td><a href="/zh-CN/docs/DOM/Entity" title="DOM/Entity">Entity</a></td>
   <td>实体名称</td>
  </tr>
  <tr>
   <td><a href="/zh-CN/docs/DOM/EntityReference" title="DOM/EntityReference">EntityReference</a></td>
   <td>实体引用名称</td>
  </tr>
  <tr>
   <td><a href="/zh-CN/docs/DOM/Notation" title="DOM/Notation">Notation</a></td>
   <td>Notation名称</td>
  </tr>
  <tr>
   <td><a href="/zh-CN/docs/DOM/ProcessingInstruction" title="DOM/ProcessingInstruction">ProcessingInstruction</a></td>
   <td>
    <p>等同于 <code><a href="/zh-CN/docs/DOM/ProcessingInstruction.target" title="DOM/ProcessingInstruction.target">ProcessingInstruction.target</a></code> 属性的值</p>
   </td>
  </tr>
  <tr>
   <td><a href="/zh-CN/docs/DOM/Text" title="DOM/Text">text</a></td>
   <td>"#text"</td>
  </tr>
 </tbody>
</table>
<h2 id="示例">示例</h2>
<p>假设已经存在下面的HTML:</p>
<pre class="brush:html">&lt;div id="d1"&gt;hello world&lt;/div&gt;
&lt;input type="text" id="t"/&gt;
</pre>
<p>以及下面的JavaScript:</p>
<pre class="brush:js">var div1 = document.getElementById("d1");
var text_field = document.getElementById("t");

text_field.value = div1.nodeName;
</pre>
<p>在XHTML(以及属于XML类型的文档)中,<code>变量text_field</code>包含的值会是小写的"div".还在HTML中,<code>变量text_field</code>包含的值会是大写的"DIV",<code>nodeName</code><code>tagName</code>属性都有这种表现.查看<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>如果是元素节点,<code>nodeName</code>属性和<code>tagName</code>属性返回相同的值,但如果是文本节点,<code>nodeName</code>属性会返回<code>"#text"</code>,而<code>tagName</code>属性会返回<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>