对文档的操作也就是,在标签中添加标签
内部插入
1.方式1
- append a.append(c) 把c插入到a内部的后面
- prepend a.prepend(c) 把c插入到a内部的前面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>内部插入节点-方式1</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//1.在city的后面内部追加 反恐
$("#city").append($("#fk"));
//2.在city的前面内部插入 反恐
$("#city").prepend($("#fk"));
});
</script>
</head>
<body>
<ul id="city">
<li id="bj" name="beijing">北京</li>
<li id="tj" name="tianjin">天津</li>
<li id="cq" name="chongqing">重庆</li>
</ul>
<ul id="love">
<li id="fk" name="fankong">反恐</li>
<li id="xj" name="xingji">星际</li>
</ul>
<div id="foo1">Hello1</div>
</body>
</html>
2.方式2
- appendTo a.appendTo(c) 将a插入到c内部的后面
- prependTo a.prependTo(c) 将a插入到c内部的前面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>内部插入节点-方式2</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//1.在city的后面内部追加 反恐
$("#fk").appendTo($("#city"));
//2.在city的前面内部插入 反恐
$("#fk").prependTo("#city");
});
</script>
</head>
<body>
<ul id="city">
<li id="bj" name="beijing">北京</li>
<li id="tj" name="tianjin">天津</li>
<li id="cq" name="chongqing">重庆</li>
</ul>
<ul id="love">
<li id="fk" name="fankong">反恐</li>
<li id="xj" name="xingji">星际</li>
</ul>
<div id="foo1">Hello1</div>
</body>
</html>
外部插入
1.方式1
- after: a.after(c) 把c插入到a外部的后面
- before: a.before(c) 把c插入到a外部的前面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>外部插入节点-方式1</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//1.在 p2 的后面插入 city
$("#p2").after($("#city"));
//2.在 p2 的前面插入 city
$("#p2").before($("#city"));
});
</script>
</head>
<body>
<ul id="city">
<li id="bj" name="beijing">北京</li>
<li id="tj" name="tianjin">天津</li>
<li id="cq" name="chongqing">重庆</li>
</ul>
<p id="p3">I would like to say: p3</p>
<p id="p2">I would like to say: p2</p>
<p id="p1">I would like to say: p1</p>
</body>
</html>
2.方式2
- insertAfter: a.insertAfter(c) 将a插入到c外部的后面
- insertBefore: a.insertBefore(c) 将a插入到c外部的前面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>外部插入节点-方式2</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
//1.在 p2 的后面插入 city
$("#city").insertAfter($("#p2"));
//2.在 p2 的前面插入 city
$("#city").insertBefore($("#p2"));
});
</script>
</head>
<body>
<ul id="city">
<li id="bj" name="beijing">北京</li>
<li id="tj" name="tianjin">天津</li>
<li id="cq" name="chongqing">重庆</li>
</ul>
<p id="p3">I would like to say: p3</p>
<p id="p2">I would like to say: p2</p>
<p id="p1">I would like to say: p1</p>
</body>
</html>
删除