2014-02-17 61 views
0

我知道,我的問題需要多一點澄清,所以讓我解釋一下:如何創建帶有括號自定義jQuery函數及參數

當我參觀了jQuery API文檔$ .fn.extend,我是

$('input[type="checkbox"]').check(); 

鏈接在這裏:由是多麼容易創建一個自定義jQuery函數類似這樣的個人吹走http://api.jquery.com/jquery.fn.extend/

但是,如果你想這樣的功能添加到功能(雙關語意):

check({ timer: 1000, redo: 4000, displayAt: 'someID' }); 

// filler parameters 

你會怎麼做到的?

而且,你會如何創建一個像check({ ... });一個功能,不依賴於任何特定jQuery選擇,而是可能只是被稱爲像這樣的常規功能:

displayRandomNumber({ min: 0, max: 100, displayAt: 'someID'}); 

// so no jquery selector in front. 

感謝您的幫助!

+0

可能的重複[如何使一個jQuery插件函數可以獨立使用,不會對集合進行操作](http:// stac koreflow.com/questions/12419658/how-to-make-a-jquery-plugin-function-callable-for-stand-alone-use-that-does-not) – terales

+1

只需創建一個常規功能。 –

回答

0

如果你想使用:

check({ timer: 1000, redo: 4000, displayAt: 'someID' }); 

與1.3中的函數定義應該像這樣:

check: function(options) { 
    // Get the timer value 
    var timerValue = options.timer; 
    // Get the redo value 
    var redoValue = options.redo; 
    ... 
} 

獨立的,它應該是這樣的:

function check(options) { 
    // Get the timer value 
    var timerValue = options.timer; 
    // Get the redo value 
    var redoValue = options.redo; 
    ... 
} 
相關問題