我正在编写这个 jquery 代码:
$('form').after('<p id="suc"></p>');
$('#suc').html('success !');
$('#suc').show(700);
setTimeout(function(){$('#suc').hide('slow')},2500);
$('#suc').remove();
当我像这样删除 $('#suc').remove();
时:
$('form').after('<p id="suc"></p>');
$('#suc').html('success !');
$('#suc').show(700);
setTimeout(function(){$('#suc').hide('slow')},2500);
代码运行成功,但是当我放置它时,它没有运行!!
这有什么问题吗?
但 $('#suc').remove();
在这里是非法的吗?
最佳答案
setTimeout
调用不会在代码继续之前等待回调运行,因此您将立即删除该元素。当回调中的代码尝试隐藏该元素时,它不在那里。
删除 hide
method 的 complete
回调中的元素:
setTimeout(function(){
$('#suc').hide('slow', function(){
$('#suc').remove();
});
},2500);
关于javascript - 使用 ".remove()"函数时代码未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14703617/