二者差别并不大!
在jq文档中,显示二者不同之处在于:内容的位置和选择器。
示例如下:
append的写法为:
(要插入的对象).append(插入什么内容)
appendTo的写法为:
(插入什么内容).appendTo(要插入的对象)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<title>append 和 appendto的区别</title>
</head>
<style>
body{
width: 100wh;
height: 100vh;
font-size: #111;
}
#appendBox{
padding: 20px;
border: 2px solid #0af1bd;
margin-bottom: 30px;
}
#appendToBox{
padding: 20px;
border: 2px solid #fb6f6f;
}
</style>
<body>
<div id="appendBox">append盒子</div>
<div id="appendToBox">appendTo盒子</div>
<script>
$(document).ready(function(){
$("#appendBox").append("<p>追加的内容 - appendBox </p>");
$("<p>追加的内容 - appendToBox </p>").appendTo("#appendToBox");
})
</script>
</body>
</html>