Jquery动态调用函数

时间:2011-10-01 16:38:54

标签: javascript jquery

我有一个以下场景,我必须动态调用jquery对象上的函数。代码如下所示:

$('div[class]').each(function(){
    var class=$(this).attr('class');

    // I want to now check if that function with the name of class exists for jquery
    // object. I need help here.
    // if that function exists, i need to apply that function to the enumerated object.
});

我现在想检查jQuery对象是否存在具有类名的函数。我需要帮助。如果该函数存在,我需要将该函数应用于枚举对象。

2 个答案:

答案 0 :(得分:2)

我正在写这篇文章,所以它可能不起作用,但请尝试:

if(jQuery.isFunction(jQuery[class]))
     jQuery[class](this);

编辑:如果函数是jquery方法,那么尝试使用:

 if(jQuery.isFunction(jQuery.fn[class])) {
     jQuery.fn[class](this);
     jQuery(this)[class]();    // alternative call syntax
 }

答案 1 :(得分:0)

使用$.isFunction(func)确定func是否为函数。

相关问题