gpt4 book ai didi

jquery - click() 函数中的 click() 函数不起作用

转载 作者:行者123 更新时间:2023-12-01 03:36:55 26 4
gpt4 key购买 nike

问题是,当我单击 delaccbut 按钮时,单击功能起作用并显示消息,但是当我单击 confdelredel 按钮来自 click 函数,它不...为什么?

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!");
});
});

这里出了什么问题?谢谢!

最佳答案

脚本初始化时,"is"和“否”按钮不存在。您需要将事件委托(delegate)给父级,以便可以将其应用于特定的子级,无论它们现在还是将来存在。

参见: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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com