gpt4 book ai didi

javascript - 我无法在 jquery 中访问我的闭包函数

转载 作者:行者123 更新时间:2023-11-30 07:07:23 27 4
gpt4 key购买 nike

如何从我的谈话事件中调用 sayIt 函数。我整夜都在与它作斗争。

$(document).ready(function () {


var sayIt = (function() {
alert("I can now say something.");
})();


$("#talk").blur(function(){

sayIt(); //<-- Uncaught TypeError: sayIt is not a function
});

});

最佳答案

sayIt 不是函数,因为您将该函数声明为自调用函数,sayIt 只是存储它返回的值。

$(document).ready(function () {


var sayIt = function() {
alert("I can now say something.");
};

sayIt(); // it you want to call it once

$("#talk").blur(function(){

sayIt(); //<-- Uncaught TypeError: sayIt is not a function
});

});

关于javascript - 我无法在 jquery 中访问我的闭包函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49569024/

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