0

I am trying to wrap my head around plugins. The code I am converting is as follows:

function parseNum(str) {
    var mdy = str.split('/')
    return new Date(mdy[2], mdy[0]-1, mdy[1]);
}

function daydiff(first, second) {
    return (second-first)/(1000*60)
}


$("#p").text((daydiff(parseNum($('#first').val()), parseNum($('#second').val()))));

This is what I have so far

;(function ($) {

    $.fn.myPlugin = function (options) {    

        // define defaults
        var defaults = {
            x1: 2/27/2013,
            x2: 2/29/2013
        };

        var o = $.extend(defaults, options);

What do I write inside the plugin and how to call it to populate $(#p).text()?

4

1 回答 1

0

如果你这样写:

;(function($) {
    $.fn.myPlugin = function(sel1, sel2) {
        $(this).text(daydiff(parseNum($(sel1).val()), parseNum($(sel2).val()));
    }
}

你可以做:

$("#p").myPlugin('#first', '#second');

如果您希望能够使用选项来增强您的插件,请将其作为第三个可选参数,并使用您编写的习惯用法将其与默认值合并。

于 2013-11-08T01:43:38.327 回答