2011-03-10 78 views
1

我已經遇到過這個問題幾次,所以這次我想我只是從那些知道更好的人那裏得到一些建議。jQuery:我如何在我的函數中使用函數參數作爲函數名稱?

我基本上想用我的function(argument)作爲函數內部的函數名。

下面是一些代碼,我嘗試使用:

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

我如何能得到這個運行的任何想法?

謝謝。

回答

5

嘗試

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

Aarg! 18秒... – sdleihssirhc 2011-03-10 03:21:25

+0

@sdleihssirhc - 我很幸運;) – Anurag 2011-03-10 03:22:37

+0

非常感謝:)工程出色。 – Jannis 2011-03-10 09:29:17

1

使用下標符號:

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

嘗試

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

這是Gary Busey如何做JavaScript的。 – sdleihssirhc 2011-03-10 03:27:48

相關問題