<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<script src="http://www.xianlaiwan.cn/static/lib/jquery/1.9.1/jquery.js"></script>
<style>
.aaron1{
border: 1px solid red;
}
.aaron1 p {
color: red;
}
.aaron2{
border: 1px solid blue;
}
.aaron2 p {
color: blue;
}
</style>
</head>
<body>
<h2>通過prepend與prependTo添加元素</h2>
<button id="bt1">點擊通過jQuery的prepend添加元素</button>
<button id="bt2">點擊通過jQuery的prependTo添加元素</button>
<div class="aaron1">
<p>測試prepend</p>
</div>
<div class="aaron2">
<p>測試prependTo</p>
</div>
<script type="text/javascript">
$("#bt1").on('click', function() {
//找到class="aaron1"的div節點
//然后通過prepend在內部的首位置添加一個新的p節點
$('.aaron1')
.prepend('<p>prepend增加的p元素</p>')
})
</script>
<script type="text/javascript">
$("#bt2").on('click', function() {
//找到class="aaron2"的div節點
//然后通過prependTo內部的首位置添加一個新的p節點
$('<p>prependTo增加的p元素</p>')
.prependTo($('.aaron2'))
})
</script>
</body>
</html>