问题是,当我点击delaccbut
按钮时,点击功能会起作用并显示消息,但当我点击点击功能中的confdel
或redel
按钮时,不......为什么?
HTML:
<span id='delaccspan'>
<button class='defbutt' id='delaccbut'>Delete my account</button>
</span>
jQuery:
$(document).ready(function(){
$("#delaccbut").click(function(){
$("#delaccspan").html("All your posts, pictures, followers and everything you did on this page will be deleted. Do you want to continue? <button class='smallbut' id='confdel'>Yes</button> <button class='smallbut' id='redel'>No</button>");
});
$("#confdel").click(function(){
$("#delaccspan").html("Okay");
});
$("#redel").click(function(){
$("#delaccspan").html("Good decision!");
});
});
这里有什么问题?谢谢!
答案 0 :(得分:3)
初始化脚本时,yes和no按钮不存在。您需要将事件委托给父母,以便它可以应用于现在或将来存在的特定儿童。
请参阅:http://learn.jquery.com/events/event-delegation/
$(function(){
$("#delaccbut").click(function(){
$("#delaccspan").html("All your posts, pictures, followers and everything you did on this page will be deleted. Do you want to continue? <button class='smallbut' id='confdel'>Yes</button> <button class='smallbut' id='redel'>No</button>");
});
$("body").on("click", "#confdel", function(){
$("#delaccspan").html("Okay");
});
$("body").on("click", "#redel", function(){
$("#delaccspan").html("Good decision!");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id='delaccspan'>
<button class='defbutt' id='delaccbut'>Delete my account</button>
</span>