aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/range/surroundcontents/index.html
blob: 58695a43f42df62e016908440a7dd8fda5ec625b (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
---
title: Range.surroundContents
slug: Web/API/Range/surroundContents
tags:
  - API
  - DOM
  - Range
  - 方法
  - 范围
translation_of: Web/API/Range/surroundContents
---
<div>{{ ApiRef("Range") }}</div>

<p><strong><code>Range.surroundContents()</code> </strong>方法将 {{ domxref("Range") }} 对象的内容移动到一个新的节点,并将新节点放到这个范围的起始处。</p>

<p>这个方法与 <code>newNode.appendChild(<a href="/en/DOM/range.extractContents" title="en/DOM/range.extractContents">range.extractContents()</a>); <a href="/en-US/docs/DOM/range.insertNode" title="/en-US/docs/DOM/range.insertNode">range.insertNode</a>(newNode)</code> 等价。应用以后, <code>newNode</code> 包含在 <code>range</code> 的边界点中。</p>

<p>然而,如果 {{ domxref("Range") }} 断开了一个非 {{ domxref("Text") }} 节点,只包含了节点的其中一个边界点,就会抛出异常。也就是说,不像上述的等价方法,如果节点仅有一部分被选中,则不会被克隆,整个操作会失败。</p>

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

<pre class="syntaxbox"><em>range</em>.surroundContents(<em>newParent</em>);
</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt><code>newParent</code></dt>
 <dd>一个包含内容的 {{ domxref("Node") }}</dd>
</dl>

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

<h3 id="HTML">HTML</h3>

<pre>&lt;span class="header-text"&gt;Put this in a headline&lt;/span&gt;</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre>const range = document.createRange();
const newParent = document.createElement('h1');

range.selectNode(document.querySelector('.header-text'));
range.surroundContents(newParent);</pre>

<h3 id="结果">结果</h3>

<p><iframe class="live-sample-frame sample-code-frame" frameborder="0" id="frame_Example" src="https://mdn.mozillademos.org/en-US/docs/Web/API/Range/surroundContents$samples/Example?revision=1549261"></iframe></p>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">注释</th>
  </tr>
  <tr>
   <td>{{SpecName('DOM WHATWG', '#dom-range-surroundcontents', 'Range.surroundContents()')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>No change.</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM2 Traversal_Range', 'ranges.html#Level2-Range-method-surroundContents', 'Range.surroundContents()')}}</td>
   <td>{{Spec2('DOM2 Traversal_Range')}}</td>
   <td>Initial specification.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("api.Range.surroundContents")}}</p>

<h2 id="参见">参见</h2>

<ul>
 <li><a href="/en-US/docs/DOM/DOM_Reference" title="/en-US/docs/DOM/DOM_Reference">The DOM interfaces index</a></li>
</ul>