开发者

Jquery call function dynamically

开发者 https://www.devze.com 2023-04-09 15:33 出处:网络
I have a following scenario that I have to call a function on a jquery object dynamically. The code looks like this :

I have a following scenario that I have to call a function on a jquery object dynamically. The code looks like this :

$('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.
});

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.


I'm writing this of the top of my head so it might not work, but try:

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

Edit: if the function is a jquery method then try with:

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

精彩评论

暂无评论...
验证码 换一张
取 消