开发者

how to call jquery function by its name [duplicate]

开发者 https://www.devze.com 2023-03-03 11:02 出处:网络
This question already has answers here: 开发者_如何学编程 jQuery call function from a string [duplicate]
This question already has answers here: 开发者_如何学编程 jQuery call function from a string [duplicate] (3 answers) Closed 9 years ago.

How to call jQuery function by its name. For example:

var fn = 'hide',
    myObj = $("#smth");

// here I want to hide myObj ( $("#smth").hide() )

// my variants were:
// fn.call(myObj) - doesn't work
// myObj.fn() - doesn't work (I've not expected, just tried =) )


Access the function as you would access any other member of myObj using a variable name, and then simply call it:

var fn = 'hide',
myObj = $("#smth");
(myObj[fn])();


Do this:

var fn = 'hide',
myObj = $("#smth");

myObj[fn]();

Cheers

0

精彩评论

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