aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/node/haschildnodes/index.html
blob: e833c72e84ca6a007d3c3a21c07712e97e90ed46 (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
---
title: Node.hasChildNodes
slug: Web/API/Node/hasChildNodes
translation_of: Web/API/Node/hasChildNodes
---
<div>{{ApiRef}}</div>

<h3 id="概述">概述</h3>

<p><strong>hasChildNodes</strong>方法返回一个<a href="/zh-CN/docs/JavaScript/Reference/Global_Objects/Boolean" title="JavaScript/Reference/Global_Objects/Boolean">布尔值</a>,表明当前<a href="/zh-CN/docs/DOM/Node" title="DOM/Node">节点</a>是否包含有<a href="/zh-CN/docs/DOM/Node.childNodes" title="DOM/Node.childNodes">子节点</a>.</p>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre class="syntaxbox"><code>element.hasChildNodes()</code></pre>

<h2 id="Example" name="Example">例子</h2>

<p>下面的例子演示了:如果id为foo的这个元素有子节点,则从dom树中删除它的第一个子节点.</p>

<pre class="brush:js;highlight:[3];">var foo = document.getElementById("foo");

if ( foo.hasChildNodes() ) {
  foo.removeChild( foo.childNodes[0] );
}</pre>

<div class="note">
<p>注意:<code>Node.hasChildNodes</code>是个方法,而不是普通属性,使用时必须加括号才能调用.</p>
</div>

<h2 id="Specification" name="Specification">总结</h2>

<p>有三种方法可以判断当前节点是否有子节点。</p>

<ul>
 <li>node.firstChild !== null</li>
 <li>node.childNodes.length &gt; 0</li>
 <li>node.hasChildNodes()</li>
</ul>

<h2 id="Specification" name="Specification">规范</h2>

<ul>
 <li><a href="https://dom.spec.whatwg.org/#dom-node-haschildnodes">WHATWG: hasChildNodes</a></li>
 <li><a class="external" href="http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-810594187">hasChildNodes</a></li>
</ul>

<h2 id="See_also" name="See_also">相关链接</h2>

<ul>
 <li>{{domxref("Node.childNodes")}}</li>
 <li>{{domxref("Node.hasAttributes")}}</li>
</ul>