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 Element.prepend() method #25223

Merged
merged 2 commits into from
Dec 31, 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
63 changes: 34 additions & 29 deletions files/zh-cn/web/api/element/prepend/index.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,78 @@
---
title: Element.prepend()
title: Elementprepend() 方法
slug: Web/API/Element/prepend
l10n:
sourceCommit: bd15d43260b7e72b1066c04d9d9f3b79129c619c
---

{{APIRef("DOM")}}

**`Element.prepend`** 方法可以在父节点的第一个子节点之前插入一系列{{domxref("Node")}}对象或者{{domxref("DOMString")}}对象。{{domxref("DOMString")}}会被当作{{domxref("Text")}}节点对待(也就是说插入的不是 HTML 代码)
**`Element.prepend()`** 方法将在 {{domxref("Element")}} 的第一个子节点之前插入一组 {{domxref("Node")}} 对象或字符串。字符串将作为等效的 {{domxref("Text")}} 节点插入
T34-active marked this conversation as resolved.
Show resolved Hide resolved

## 语法

```plain
Element.prepend((Node or DOMString)... nodes);
```js-nolint
prepend(param1)
prepend(param1, param2)
prepend(param1, param2, /* …, */ paramN)
```

### 参数

- `nodes`
- : 要插入的一系列{{domxref("Node")}}或者{{domxref("DOMString")}}
- `param1`, …, `paramN`
T34-active marked this conversation as resolved.
Show resolved Hide resolved
- : 一组要插入的 {{domxref("Node")}} 对象或字符串

### 返回值

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

### 异常

- {{domxref("HierarchyRequestError")}}:节点不能插入当前层级内。
- `HierarchyRequestError` {{DOMxRef("DOMException")}}
- : 当节点无法插入到指定的层级位置时抛出。

## 示例

### Prepending an element
### 在元素前插入

```js
var parent = document.createElement("div");
var p = document.createElement("p");
var span = document.createElement("span");
parent.append(p);
parent.prepend(span);
let div = document.createElement("div");
let p = document.createElement("p");
let span = document.createElement("span");
div.append(p);
div.prepend(span);

console.log(parent.childNodes); // NodeList [ <span>, <p> ]
console.log(div.childNodes); // 节点列表 [ <span>, <p> ]
T34-active marked this conversation as resolved.
Show resolved Hide resolved
```

### Prepending text
### 在文本前插入

```js
var parent = document.createElement("div");
parent.append("Some text");
parent.prepend("Headline: ");
let div = document.createElement("div");
div.append("一些文本");
div.prepend("标题:");

console.log(parent.textContent); // "Headline: Some text"
console.log(div.textContent); // "标题:一些文本"
T34-active marked this conversation as resolved.
Show resolved Hide resolved
```

### Appending an element and text
### 在元素前插入元素和文本

```js
var parent = document.createElement("div");
var p = document.createElement("p");
parent.prepend("Some text", p);
let div = document.createElement("div");
let p = document.createElement("p");
div.prepend("一些文本", p);

console.log(parent.childNodes); // NodeList [ #text "Some text", <p> ]
console.log(div.childNodes); // 节点列表 [ #text "一些文本", <p> ]
T34-active marked this conversation as resolved.
Show resolved Hide resolved
```

### `Element.prepend()` is unscopable
### prepend 方法是不可访问的
jasonren0403 marked this conversation as resolved.
Show resolved Hide resolved
T34-active marked this conversation as resolved.
Show resolved Hide resolved

`prepend()` 不能在 with 语句内使用,详情参考 {{jsxref("Symbol.unscopables")}}。
`prepend()` 方法不在 `with` 语句中作用域内。有关更多信息,请参阅 {{jsxref("Symbol.unscopables")}}。
T34-active marked this conversation as resolved.
Show resolved Hide resolved

```js
var parent = document.createElement("div");
let div = document.createElement("div");

with (parent) {
with (div) {
prepend("foo");
}
// ReferenceError: prepend is not defined
Expand Down
Loading