gpt4 book ai didi

javascript - 执行后, window.setTimeout() 抛出 'Uncaught TypeError: undefined is not a function' ?

转载 作者:行者123 更新时间:2023-11-29 19:37:04 27 4
gpt4 key购买 nike

下面是我的问题代码:

// if any alerts, set them to auto-close
window.setTimeout(function() {
$.each( $('.alert'), function(){
closeAlert( this );
});
}, 2000);

function closeAlert( alert ){
$(alert).fadeTo(2000, 500).slideUp(500, function(){
$(alert).close();
});
};

所以上面的代码用于在几秒钟后自动关闭 Bootstrap 警报。它工作得很好,但是在 closeAlert() 方法关闭警报后,我的控制台立即收到以下错误:

未捕获的 TypeError:undefined 不是函数错误引用了 closeAlert() 方法的最后一个括号。

为什么在定义函数时出现未定义错误?

最佳答案

这个

$(alert).close();

应该是这样

$(alert).hide();

出现错误是因为 Jquery 没有名为 .close() 的函数


完整代码是,

window.setTimeout(function() {
$.each( $('.alert'), function(){
closeAlert( this );
});
}, 2000);

function closeAlert( alert ){
$(alert).fadeTo(2000, 500).slideUp(500, function(){
$(alert).hide();
});
};

关于javascript - 执行后, window.setTimeout() 抛出 'Uncaught TypeError: undefined is not a function' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24867510/

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