Passing function name as a parameter to another function似乎对我不起作用。
我已经尝试了我能找到的每篇文章的所有变体。目前,我在一个 js 文件中有这个:
function callThisPlease (testIt){
alert(testIt);
}
$(document).ready(function () {
$.fn.pleaseCallTheOtherFunction('callThisPlease');
});
我有另一个:
$(document).ready(function () {
$.fn.pleaseCallTheOtherFunction = function(functionName){
window[functionName].apply('works');
}
});
chrome 控制台显示 Uncaught TypeError: Cannot call method 'apply' of undefined
。
请帮忙。非常感谢!
最佳答案
如果方法在 window
上未定义,则意味着您的函数不是全局的。使其成为一个全局函数。
此外,您可以去掉 .apply
。当前,您正在将 'works'
作为 this
值传递。
window[functionName]('works');
关于javascript - 将参数传递给作为参数传递给 jQuery 函数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14835744/