簡體   English   中英

如何使用方括號和參數創建自定義jquery函數

[英]How to create a custom jquery function with brackets and parameters

我知道我的問題需要更多說明,所以讓我解釋一下:

當我訪問$ .fn.extend的jQuery api文檔時,我個人被創建這樣的自定義jquery函數如此容易感到震驚:

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

鏈接在這里: http : //api.jquery.com/jquery.fn.extend/

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

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

// filler parameters

確切地說,您將如何做?

另外,如何創建類似check({ ... });的函數check({ ... }); 它不依賴於任何特定的jquery選擇器,而是可以像常規函數一樣調用:

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

// so no jquery selector in front.

謝謝你的幫助!

如果要使用:

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

使用jquery.fn.extend可以像下面這樣聲明您的函數:

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;
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM