2019-02-14 14:59:19 +00:00
|
|
|
|
---
|
|
|
|
|
title: HTML Method
|
|
|
|
|
localeTitle: HTML方法
|
|
|
|
|
---
|
2018-10-12 20:35:31 +00:00
|
|
|
|
|
2019-02-14 14:59:19 +00:00
|
|
|
|
# HTML方法
|
|
|
|
|
|
|
|
|
|
jQuery `.html()`方法获取HTML元素的内容或设置HTML元素的内容。
|
2018-10-12 20:35:31 +00:00
|
|
|
|
|
|
|
|
|
## 入门
|
|
|
|
|
|
|
|
|
|
要返回HTML元素的内容,请使用以下语法:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
$('selector').html();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
例如:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
$('#example').html();
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## 设置
|
|
|
|
|
|
|
|
|
|
要设置HTML元素的内容,请使用以下语法:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
$('selector').html(content);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
例如:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
$('p').html('Hello World!');
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
这会将所有`<p>`元素的内容设置为Hello World!
|
|
|
|
|
|
|
|
|
|
### 更多信息
|
|
|
|
|
|
2019-02-14 14:59:19 +00:00
|
|
|
|
[W3Schools的](https://www.w3schools.com/jquery/html_html.asp)
|