将Jquery函数变成插件

发布于 2024-12-26 10:30:52 字数 2484 浏览 0 评论 0原文

我最近收到了一些关于编写 jquery 功能的极好的建议。我正在尝试将其变成一个插件。
这是我通过此链接从 stackOverflow@micha 获得的代码

https://stackoverflow.com/a/8820946/729820

$('#DischargeDateTimeMask').keypress(function (e) {
        var regex = ["[0-1]",
                     "[0-2]",
                     ":",
                     "[0-5]",
                     "[0-9]",
                     "(\\s)",
                     "(A|P)",
                     "M"],
        string = $(this).val() + keyboard(e),
        b = true;
        for (var i = 0; i < string.length; i++) {
            if (!new RegExp("^" + regex[i] + "$").test(string[i])) {
                b = false;
            }
        }
        return b;
    });
    function keyboard(a) { var b = a.charCode ? a.charCode : a.keyCode ? a.keyCode : 0; if (b == 8 || b == 9 || b == 13 || b == 35 || b == 36 || b == 37 || b == 39 || b == 46) { if ($.browser.mozilla) { if (a.charCode == 0 && a.keyCode == b) { return true } } } return String.fromCharCode(b) }

我个人尝试将其变成一个插件,如下所示...

(function ($) {
$.fn.simpleTimeMask = function () {
    $(this).keypress(function (e) {
        debugger;
        var regex = ["[0-2]",
        "[0-4]",
        ":",
        "[0-6]",
        "[0-9]",
        "(A|P)",
        "M"],
        string = $(this).val() + keyboard(e),
        b = true;
        for (var i = 0; i < string.length; i++) {
            if (!new RegExp("^" + regex[i] + "$").test(string[i])) {
                b = false;
            }
        }
        return b;
    });
}


    (function ($) {
        var methods = {
            keyboard: function (a) {
                //THIS
                var b = a.charCode ? a.charCode : a.keyCode ? a.keyCode : 0; if (b == 8 || b == 9 || b == 13 || b == 35 || b == 36 || b == 37 || b == 39 || b == 46) { if ($.browser.mozilla) { if (a.charCode == 0 && a.keyCode == b) { return true } } } return String.fromCharCode(b)
            }
        }
    });

function keyboard(a) { var b = a.charCode ? a.charCode : a.keyCode ? a.keyCode : 0; if (b == 8 || b == 9 || b == 13 || b == 35 || b == 36 || b == 37 || b == 39 || b == 46) { if ($.browser.mozilla) { if (a.charCode == 0 && a.keyCode == b) { return true } } } return String.fromCharCode(b) }
 })(jQuery);

请注意我如何有两个函数尝试执行相同的操作。我认为一个函数将由该插件的用户调用,另一个将在内部使用。不管怎样,我把这个东西全部编译好了,并尝试将它添加到我的项目中,但正如预期的那样,它不起作用。您对构建这个插件有什么建议?

I recently received some excellent advice for writing some jquery functionality. I am trying to turn it into a pluggin.
this is the code that I got from stackOverflow@micha at this link

https://stackoverflow.com/a/8820946/729820

$('#DischargeDateTimeMask').keypress(function (e) {
        var regex = ["[0-1]",
                     "[0-2]",
                     ":",
                     "[0-5]",
                     "[0-9]",
                     "(\\s)",
                     "(A|P)",
                     "M"],
        string = $(this).val() + keyboard(e),
        b = true;
        for (var i = 0; i < string.length; i++) {
            if (!new RegExp("^" + regex[i] + "$").test(string[i])) {
                b = false;
            }
        }
        return b;
    });
    function keyboard(a) { var b = a.charCode ? a.charCode : a.keyCode ? a.keyCode : 0; if (b == 8 || b == 9 || b == 13 || b == 35 || b == 36 || b == 37 || b == 39 || b == 46) { if ($.browser.mozilla) { if (a.charCode == 0 && a.keyCode == b) { return true } } } return String.fromCharCode(b) }

I personally attempt to turn it into a pluggin like so...

(function ($) {
$.fn.simpleTimeMask = function () {
    $(this).keypress(function (e) {
        debugger;
        var regex = ["[0-2]",
        "[0-4]",
        ":",
        "[0-6]",
        "[0-9]",
        "(A|P)",
        "M"],
        string = $(this).val() + keyboard(e),
        b = true;
        for (var i = 0; i < string.length; i++) {
            if (!new RegExp("^" + regex[i] + "$").test(string[i])) {
                b = false;
            }
        }
        return b;
    });
}


    (function ($) {
        var methods = {
            keyboard: function (a) {
                //THIS
                var b = a.charCode ? a.charCode : a.keyCode ? a.keyCode : 0; if (b == 8 || b == 9 || b == 13 || b == 35 || b == 36 || b == 37 || b == 39 || b == 46) { if ($.browser.mozilla) { if (a.charCode == 0 && a.keyCode == b) { return true } } } return String.fromCharCode(b)
            }
        }
    });

function keyboard(a) { var b = a.charCode ? a.charCode : a.keyCode ? a.keyCode : 0; if (b == 8 || b == 9 || b == 13 || b == 35 || b == 36 || b == 37 || b == 39 || b == 46) { if ($.browser.mozilla) { if (a.charCode == 0 && a.keyCode == b) { return true } } } return String.fromCharCode(b) }
 })(jQuery);

Notice how I have two functions that attempt to do the same thing. One I think would be a function that would be called by a user of this pluggin, the other would be used internally. Anyway, I got this thing all compiled and I attempted to add it to my project, but as expected, it did not work. What would be your recommendations for building this pluggin?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文