Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[zh-cn]: update the translation of Range.surroundContents() method #23178

Merged
merged 2 commits into from
Aug 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions files/zh-cn/web/api/range/surroundcontents/index.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
---
title: Range.surroundContents
title: RangesurroundContents() 方法
slug: Web/API/Range/surroundContents
l10n:
sourceCommit: 1a91b0b63f0cbaca9125bd48d4e5bc8afed2a7a3
---

{{ ApiRef("Range") }}
{{ApiRef("DOM")}}

**`Range.surroundContents()`** 方法将 {{ domxref("Range") }} 对象的内容移动到一个新的节点,并将新节点放到这个范围的起始处
**`Range.surroundContents()`** 方法将 {{ domxref("Range") }} 的内容移动到一个新节点,并将该新节点放置在指定的起始位置
yin1999 marked this conversation as resolved.
Show resolved Hide resolved

这个方法与 `newNode.appendChild(range.extractContents()); range.insertNode(newNode)` 等价。应用以后, `newNode` 包含在 `range` 的边界点中
此方法几乎等同于 `newNode.appendChild(range.extractContents()); range.insertNode(newNode)`。在包围操作之后,`range` 的边界点将包含 `newNode`

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

## 语法

```plain
range.surroundContents(newParent);
```js-nolint
surroundContents(newParent)
```

### 参数

- `newParent`
- : 一个包含内容的 {{ domxref("Node") }}。
- : 用于包围内容的 {{ domxref("Node") }}。

### 返回值

无({{jsxref("undefined")}})。

## 示例

### HTML

```plain
<span class="header-text">Put this in a headline</span>
```html
<span class="header-text">在标题中写道</span>
```

### JavaScript

```plain
```js
const range = document.createRange();
const newParent = document.createElement('h1');
const newParent = document.createElement("h1");

range.selectNode(document.querySelector('.header-text'));
range.selectNode(document.querySelector(".header-text"));
range.surroundContents(newParent);
```

Expand All @@ -54,4 +60,4 @@ range.surroundContents(newParent);

## 参见

- [The DOM interfaces index](/zh-CN/docs/DOM/DOM_Reference)
- [DOM 接口索引](/zh-CN/docs/Web/API/Document_Object_Model)