jQuery中,有append、prepend等方法,容易搞混淆,在这里记录一下,以便于区分
大致有如下方法,可以理解为:
- append:后置x(…后面添加)
- prepend:前置x (…前面添加)
- appendTo:后置到
- prependTo:前置到
- after:后面是
- before:前面是
- insertAfter:插入到…后面
- insertBefore:插入到…前面
具体案例如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>内部插入脚本</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="../js/jquery-3.3.1.min.js"></script>
<style type="text/css">
div,span{
width: 140px;
height: 140px;
margin: 20px;
background: #9999CC;
border: #000 1px solid;
float:left;
font-size: 17px;
font-family:Roman;
}
div .mini{
width: 30px;
height: 30px;
background: #CC66FF;
border: #000 1px solid;
font-size: 12px;
font-family:Roman;
}
div.visible{
display:none;
}
</style>
<script type="text/javascript">
$(function(){
// 将反恐放置到city的后面
$("#b1").click(function (){
$("#city").append($("#fk"));
//$("#fk").appendTo($("#city"));
});
// 将反恐放置到city的最前面
$("#b2").click(function (){
// $("#city").prepend($("#fk"));
$("#fk").prependTo($("#city"));
});
// 将反恐插入到天津后面
$("#b3").click(function (){
//$("#fk").insertAfter($("#tj"));
$("#tj").after($("#fk"));
});
// 将反恐插入到天津前面
$("#b4").click(function (){
//$("#fk").insertBefore($("#tj"));
$("#tj").before($("#fk"));
});
});
</script>
</head>
<body>
<input type="button" value="将反恐放置到city的后面" id="b1"/>
<input type="button" value="将反恐放置到city的最前面" id="b2"/>
<input type="button" value="将反恐插入到天津后面" id="b3"/>
<input type="button" value="将反恐插入到天津前面" id="b4"/>
<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>