开发者

jQuery: How can I use a function argument as a function name inside my function?

I have run into this problem a few times before so this time around I figured开发者_运维百科 I would simply get some advice from those that know better.

I basically want to use my function( argument ) as a function name inside this function.

Here is some code that I was trying to use:

$.fn.moveTo = function( myAction ) {

    $(this).myAction().fadeIn(400);

};
$('.currentElement').moveTo( 'next' ); // should become: $(this).next().fadeIn(400);
$('.currentElement').moveTo( 'prev' ); // should become: $(this).prev().fadeIn(400);

Any ideas on how I can get this to run?

Thanks.


Try

$(this)[myAction]().fade(400)


Use subscript notation:

$(this)[myAction]().fadeIn(400);


Try

$(this)[myAction].apply($(this), []).fadeIn(400);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜