文档操作

一 概念


1、内部插入

```
append(content|fn)
appendTo(content)
prepend(content|fn)
prependTo(content)
```

2、外部插入

```
after(content|fn)
before(content|fn)
insertAfter(content)
insertBefore(content)
```

3、包裹

```
wrap(html|ele|fn)
unwrap()
wrapAll(html|ele)
wrapInner(html|ele|fn)
```

4、替换

```
replaceWith(content|fn)
replaceAll(selector)
```

5、删除

```
empty()
remove([expr])
detach([expr])
```

6、复制

```
clone([Even[,deepEven]])

二 代码示范

<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>文档操作</title>
<style type="text/css">
.div {
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>

</body>
<script src="js/jquery-3.3.1.js"></script>
<script type="text/javascript">
$(function () {
// 1.创建
var $div = $("<div></div>");
// 2.设置
$div.addClass("div");
$div.on('click', function () {
console.log('div click');
})
// 3.添加
// $('body').append($div);
$div.appendTo($('body'));
})
</script>
</html>

posted @ 2018-10-23 16:16  不沉之月  阅读(68)  评论(0)    收藏  举报