【问题标题】:How to create a custom jquery function with brackets and parameters如何使用括号和参数创建自定义 jquery 函数
【发布时间】:2014-02-17 20:05:32
【问题描述】:

我知道我的问题需要进一步澄清,所以让我解释一下:

当我访问 $.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({ ... }); 这样不依赖任何特定 jquery 选择器的函数,而可以像这样的常规函数​​一样调用:

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

// so no jquery selector in front.

感谢您的帮助!

【问题讨论】:

标签: javascript jquery parameters extend


【解决方案1】:

如果你想使用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多