[英]Jquery call function dynamically
我有一个以下场景,我必须动态调用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对象是否存在具有类名的函数。 我需要帮助。 如果该函数存在,我需要将该函数应用于枚举对象。
我正在写这篇文章,所以它可能不起作用,但尝试:
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
}
使用$.isFunction(func)
来确定func
是否是函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.