问题是,当我单击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!");
});
});
怎么了谢谢!
最佳答案
脚本初始化时,您的是和否按钮不存在。您需要将事件委托给父项,以便可以将其应用于当前或将来存在的特定子项。
参见: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>
关于jquery - click()函数中的click()函数不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31210576/