這段代碼哪里錯了 為什么prependTo()點(diǎn)擊沒反映?
<!DOCTYPE html>
<html lang="en">
<head>
? ?<meta charset="UTF-8">
? ?<title>環(huán)境搭建</title>
? ?<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
? ?<style type="text/css">
? ? ? ?.aaron1{
? ? ? ? ? ?border: 1px solid red;
? ? ? ?}
? ? ? ?.aaron1 p{
? ? ? ? ? ?color: red;
? ? ? ?}
? ? ? ?.aaron2{
? ? ? ? ? ?border:1px solid blue ;
? ? ? ?}
? ? ? ?.aaron p{
? ? ? ? ? ?color:blue;
? ? ? ?}
? ?</style>
</head>
<body>
<h2>通過prepend與prependTo添加元素</h2>
<button id="btn1">點(diǎn)擊通過jQueryd的prepend添加元素</button>
<button id="btn2">點(diǎn)擊通過jQueryd的prependTo添加元素</button>
<div class="aaron1">
? ?<p>測試prepend</p>
</div>
<div class="aaron2">
? ?<p>測試prepend</p>
? ?<script type="text/javascript">
? ? ? ?$("#btn1").on("click",function () {
? ? ? ? ? ?$(".aaron1").prepend("<p>prepend增加的p元素</p>")
? ? ? ?})
? ?</script>
? ?<script type="text/javascript">
? ? ? ?$("#btn2").on("click",function () {
? ? ? ? ? ?$("<p>prependTo添加的元素</p>").prependTo($("aaron2"))
? ? ? ?})
? ?</script>
</div>
</body>
</html>
2016-12-14
<script type="text/javascript">
?????? $("#btn1").on("click",function () {
?????????? $(".aaron1").prepend("<p>prepend增加的p元素</p>")
?????? }), $("#btn2").on("click",function () {
?????????? $("<p>prependTo添加的元素</p>").prependTo($("aaron2"))?????? })
?? </script>
上面的prependTo($("aaron2"))? 應(yīng)該寫成 prependTo(".aaron2")
2018-10-28
<script type="text/javascript">
??? $("#bt1").on('click', function() {
??????? //找到class="aaron1"的div節(jié)點(diǎn)
??????? //然后通過prepend在內(nèi)部的首位置添加一個新的p節(jié)點(diǎn)
??????? $('.aaron1')
??????????? .prepend('<p>prepend增加的p元素</p>')
??? })
??? </script>
??? <script type="text/javascript">
??? $("#bt2").on('click', function() {
??????? //找到class="aaron2"的div節(jié)點(diǎn)
??????? //然后通過prependTo內(nèi)部的首位置添加一個新的p節(jié)點(diǎn)
??????? $('<p>prependTo增加的p元素</p>')
??????????? .prependTo($('.aaron2'))
??? })
??? </script>
這樣也沒錯吧