gpt4 book ai didi

jquery - jquery 的 click() 事件

转载 作者:行者123 更新时间:2023-12-01 04:21:47 25 4
gpt4 key购买 nike

所以我有这段代码,在 AJAX 请求之后在 print_info() 函数中调用;



'print_info':函数(消息,类型){
$('.load').hide();
$('
'+message+'
').insertAfter('#loader').addClass('autoHideBox '+type).fadeIn("慢").delay(2000).fadeOut("慢", function() { $(this).remove(); });
},

正如您所见,div 在几秒钟内淡入淡出,我的问题是,即使我添加了以下代码:

.click(function() { $(this).fadeOut("slow", function() { $(this).remove(); }); } )

div 根本不会淡出/删除。你能清除这个吗?因为我对这件事有点困惑。

问候,杜鲁曼·埃迪

<小时/>

稍后编辑:

我可以使用 .on("click",callback) 来实现它,但是,淡出它并在回调中将其删除是行不通的。

所以,为了让它工作,你可以在 on("click"的回调中做任何你需要做的事情,只需在 .on() 、 stop(true,true).on("click “,回调);

'print_info' : function(message,type) {
$('.load').hide();
$('<div>'+message+'</div>')
.insertAfter('#loader')
.addClass('autoHideBox '+type)
.fadeIn("slow")
.delay(2000)
.fadeOut("slow", function() { $(this).remove(); })
.on('click', function() {
$(this).stop(true,true).fadeOut("slow", function() { $(this).remove(); })
});
},

最佳答案

我认为问题在于 .click 只能附加到最初在页面加载时加载的元素。

由于您当时创建了该元素,因此它没有正确附加到单击事件。尝试改用 .on('click', callback) 因为它可以添加到页面加载后动态创建的任何内容中。

http://api.jquery.com/on/

关于jquery - jquery 的 click() 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9841488/

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