gpt4 book ai didi

javascript - Jquery动态调用函数

转载 作者:行者123 更新时间:2023-12-02 20:04:24 25 4
gpt4 key购买 nike

我有以下场景,我必须动态调用 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
}

关于javascript - Jquery动态调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7621572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com