将参数传递给作为参数传递给jQuery函数的函数 [英] pass argument to function passed as argument to jQuery function

查看:146
本文介绍了将参数传递给作为参数传递给jQuery函数的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将函数名称作为参数传递给另一个函数

a>似乎并不适合我。



我试过每篇文章中的每一个变化,我都能找到。目前,我在一个js文件中有这个:

 函数callThisThisPlease(testIt){
alert(testIt);


$(document).ready(function(){
$ .fn.pleaseCallTheOtherFunction('callThisPlease');
});

我在另一个中有这个:

<$ p $($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' 'works');
}

});

chrome控制台显示未捕获的TypeError:无法调用方法'apply'undefined



请帮忙。非常感谢!

解决方案

如果方法在窗口这意味着你的功能不是全球性的。使它成为一个全局函数。




另外,你可以去掉 .apply 。目前,您将'works'作为这个值。

  window [functionName]('works'); 


Passing function name as a parameter to another function doesn't seem to work for me.

I've tried every variation from every article I can find. Currently, I have this in one js file:

function callThisPlease (testIt){
    alert(testIt);
}

$(document).ready(function () {
    $.fn.pleaseCallTheOtherFunction('callThisPlease');
});

I have this in another:

$(document).ready(function () {

    $.fn.pleaseCallTheOtherFunction = function(functionName){
        window[functionName].apply('works');
    }

});

chrome console says Uncaught TypeError: Cannot call method 'apply' of undefined.

Please help. Many thanks in advance!

解决方案

If the method is undefined on window, that means your function isn't global. Make it a global function.


Also, you can get rid of .apply. Currently you're passing 'works' as the this value.

window[functionName]('works');

这篇关于将参数传递给作为参数传递给jQuery函数的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆