【问题标题】:Override method inside jquery widgetjQuery小部件内的覆盖方法
【发布时间】:2012-06-15 23:24:46
【问题描述】:

我正在尝试覆盖 jquery 小部件中的方法。该方法可以在https://github.com/got5/tapestry5-jquery/blob/master/src/main/resources/org/got5/tapestry5/jquery/validation.js的第122行找到

我想更改第 141 行的 html 输出

我尝试将以下内容添加到我的自定义 js 类中,但没有成功。如果有人可以解释如何做到这一点,我将不胜感激。

(function($) {    
$.widget( "ui.tapestryFieldEventManager", {
    showValidationMessage : function(message) {
        var field = this.element;
        var form = field.closest('form');

        this.options.validationError = true;
        form.formEventManager("setValidationError", true);

        field.addClass("t-error");

        this.getLabel() && this.getLabel().addClass("t-error");

        var icon = this.getIcon();

        if (icon) icon.show();
        alert("here");
        var id = field.attr('id')+"\\:errorpopup";
        if($("#"+id).size()==0) //if the errorpopup isn't on the page yet, we create it
            field.after("<div id='"+field.attr('id')+":errorpopup' class='tjq-error-popup test'/>");
        Tapestry.ErrorPopup.show($("#"+id),"<span>"+message+"</span>");

    }
});
})(jQuery);

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    你必须在原型上覆盖它。

    var oldMethod = $.ui.tapestryFieldEventManager.prototype.showValidationMessage;    
    $.ui.tapestryFieldEventManager.prototype.showValidationMessage = function (message) {
        // do your stuff here
        alert("worky");
    
        // apply old method
        oldMethod.apply(this,arguments);
    };
    

    当然,如果您的新方法完成旧方法所做的一切,您可以跳过应用旧方法。

    【讨论】:

    • 可能只是我,但我不认为这种确切的方法适用于 jQuery UI 1.9.x。
    猜你喜欢
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    相关资源
    最近更新 更多