【问题标题】:How to override event.stopPropagation(),preventDefault().stopImmediatePropagation()如何覆盖 event.stopPropagation(),preventDefault().stopImmediatePropagation()
【发布时间】:2014-12-05 17:08:49
【问题描述】:

我正在尝试修改 Jira Tempo 插件的计划工作表。所以在节奏时间表页面 我有一个按钮,它有两个绑定的动作,一个是点击动作,一个是焦点。 单击操作会打开一个弹出窗口。焦点输出验证弹出窗口中的表单。我需要先执行单击操作,然后再执行焦点。

问题是当弹出窗口打开时,会调用所有三个函数。 event.stopPropagation().preventDefault().stopImmediatePropagation(); 所以焦点不会在 chrome 中触发。

在 IE10 和 Firefox 中运行良好。

我可以覆盖event.stopPropagation().preventDefault().stopImmediatePropagation() 的jQuery 基本函数不申请那个按钮吗? 三个都可以吗?

我试过了:

(function () {
jQuery.Event.prototype = {
    isDefaultPrevented: returnFalse,
isPropagationStopped: returnFalse,
isImmediatePropagationStopped: returnFalse,
    stopPropagation: function () {
        if (jQuery('#tempo-plan-button').hasClass(this.currentTarget.className)) {

        } else {
            var e = this.originalEvent;

            this.isPropagationStopped = returnTrue;

            if (e && e.stopPropagation) {
                e.stopPropagation();
            }
        }
    },
    preventDefault: function () {
        if (jQuery('#tempo-plan-button').hasClass(this.currentTarget.className)) {

        } else {
            var e = this.originalEvent;

            this.isDefaultPrevented = returnTrue;

            if (e && e.preventDefault) {
                e.preventDefault();
            }
        }
    },
    stopImmediatePropagation: function () {
        if (jQuery('#tempo-plan-button').hasClass(this.currentTarget.className)) {

        } else {
            var e = this.originalEvent;

            this.isImmediatePropagationStopped = returnTrue;

            if (e && e.stopImmediatePropagation) {
                e.stopImmediatePropagation();
            }

            this.stopPropagation();
        }
    }

};
function returnFalse() {
    return false;
}
function returnTrue() {
    return true;
}
})();

更新:

我无法控制点击功能的作用,但我可以控制焦点输出,我做了一些测试,我看起来 Chrome 没有看到焦点输出事件,因为元素在发生焦点(它是一个弹出按钮)。 我设法用鼠标移出事件调用我的函数,但这在 IE 上不起作用,所以我必须为 IE 和 Chrome、Firefox、Safari 绑定单独的事件侦听器。 您能否提供一些浏览器检测示例。

更新 2:

我的问题通过弹出元素的不同侦听器解决了。例如:

jQuery(document).ready(function ($) {
var currentUser = null;
var selected = null;
var opened = null;
var formOpened = null;
var activityField = null;
var activitySelected = null;
var popupBody = null;
var formOpened = null;
var periodCheckbox = null;
var dateField = null;
var endDateField = null;
var plannedHours = null;
var periodselected = null;
var days = 1;
var startdate = 0;
var enddate = 0;
var planButton = $('#tempo-plan-button');
//this is the default LM-Holiday activity ID set this
value to corespond with the value on jira tempo form.
var holidayid = "10100";
var holidaysLeft = 21; /*
 $('#tempo-plan-button').click(function () {
 alert("click event");
 });
 $('#tempo-plan-button').focusin(function () {
 alert("focus event");
 });
 $('#tempo-plan-button').hover(function () {
 alert("hover event");

 });
 $('#tempo-plan-button').mouseleave(function () {
 alert("mouse leave event");

 });
 $('#tempo-plan-button').mouseout(function () {
 alert("mouse out event");

 }); */
$('body').one('focus', 'div[class="aui-popup aui-dialog"]', function (event) {
    $('div[class="aui-popup aui-dialog"]').each(function () {
        popupBody = $(this).find(".dialog-components");
        if ($(this).find(".dialog-title").hasClass("tempo-add-button") === false) {
            i = 0;
            j = 0;
            $(popupBody).find(".dialog-page-menu li").each(function () {
                if ($(this).attr("class") === "page-menu-item selected") {
                    button = $(this).find('.item-button');
                    if ((button).text() === "Internal") {
                        selected = i;
                    }
                }
                i++;
            });
            $(popupBody).find(".dialog-panel-body").each(function () {
                if ($(this).is(":visible")) {
                    opened = j;
                    formOpened = $(this).find('form');
                }
                j++;
            });
            if (selected === null) {
                i = 0;
                j = 0;
                $(popupBody).find(".dialog-page-menu li").click(function () {
                    $(popupBody).find(".dialog-page-menu li").each(function () {
                        if ($(this).attr("class") === "page-menu-item selected") {
                            button = $(this).find('.item-button');
                            if ((button).text() === "Internal") {
                                selected = i;
                            }
                        }
                        i++;
                    });
                    $(popupBody).find(".dialog-panel-body").each(function () {
                        if ($(this).is(":visible")) {
                            opened = j;
                            formOpened = $(this).find('form');
                        }
                        j++;
                    });
                    if (selected === opened) {
                        activityField = $(formOpened).find('.tempo-activity-picker.select');
                        periodCheckbox = $(formOpened).find('.showperiod.tempo-show-period');
                        dateField = $(formOpened).find(' input[name="date"]');
                        endDateField = $(formOpened).find('input[name="enddate"]');
                        plannedHours = $(formOpened).find('input[name="time"]');
                        days = 1;
                        $(activityField).change(function () {
                            activitySelected = $(this).val();
                        });

                        $(periodCheckbox).change(function () {
                            if ($(this).prop("checked")) {
                                periodselected = true;
                            }
                            else
                            {
                                periodselected = false;

                            }
                        });
                        $(dateField).change(function () {
                            if (periodselected) {
                                startdate = parseDate($(this).val());
                                enddate = parseDate($(endDateField).val());
                            } else {
                                days = 1;
                            }
                            ;
                        });
                        $(endDateField).change(function () {
                            startdatestring = $(dateField).val();
                            enddatestring = $(this).val();
                            startdate = parseDate(startdatestring);
                            enddate = parseDate(enddatestring);
                        });
                        $(plannedHours).off("focusin");
                        $(plannedHours).focusin(function () {
                            if (activitySelected === holidayid) {
                                if (periodselected) {
                                    days = calcBusinessDays(startdate, enddate);

                                    if (holidaysLeft >= days) {
                                        holidaysLeft = holidaysLeft - days;
                       alert("Mai ai " + holidaysLeft + " zile de concediu ramase!");
                      $(popupBody).find('button[accesskey="s"]').removeAttr('disabled');
                                    }
                                    else {
         $(popupBody).find('button[accesskey="s"]').attr('disabled', 'disabled');
         alert('trebuie sa selectezi o perioada mai mica de' + holidaysLeft + 'de zile');
                                    }
                                } else
                                {
                                    if (holidaysLeft >= days) {
                                        holidaysLeft = holidaysLeft - days;
                                 alert("Mai ai " + holidaysLeft + " zile de concediu ramase!");
                        $(popupBody).find('button[accesskey="s"]').removeAttr('disabled');
                                    }
                                    else {
                 $(popupBody).find('button[accesskey="s"]').attr('disabled', 'disabled');
               alert('trebuie sa selectezi o perioada mai mica de' + holidaysLeft + 'de zile');
                                    }
                                }
                            }
                        });
                    }
                });
            } else {
                j = 0;
                $(popupBody).find(".dialog-panel-body").each(function () {
                    if ($(this).is(":visible")) {
                        opened = j;
                        formOpened = $(this).find('form');
                    }
                    j++;
                });
                if (selected === opened) {
                    activityField = $(formOpened).find('.tempo-activity-picker.select');
                    periodCheckbox = $(formOpened).find('.showperiod.tempo-show-period');
                    dateField = $(formOpened).find(' input[name="date"]');
                    endDateField = $(formOpened).find('input[name="enddate"]');
                    plannedHours = $(formOpened).find('input[name="time"]');
                    days = 1;
                    $(activityField).change(function () {
                        activitySelected = $(this).val();
                    });

                    $(periodCheckbox).change(function () {
                        if ($(this).prop("checked")) {
                            periodselected = true;
                        }
                        else
                        {
                            periodselected = false;

                        }
                    });
                    $(dateField).change(function () {
                        if (periodselected) {
                            startdate = parseDate($(this).val());
                            enddate = parseDate($(endDateField).val());
                        } else {
                            days = 1;
                        }
                        ;
                    });
                    $(endDateField).change(function () {
                        startdatestring = $(dateField).val();
                        enddatestring = $(this).val();
                        startdate = parseDate(startdatestring);
                        enddate = parseDate(enddatestring);
                    });
                    $(plannedHours).off("focusin");
                    $(plannedHours).focusin(function () {
                        if (activitySelected === holidayid) {
                            if (periodselected) {
                                days = calcBusinessDays(startdate, enddate);

                                if (holidaysLeft >= days) {
                                    holidaysLeft = holidaysLeft - days;
               alert("Mai ai " + holidaysLeft + " zile de concediu ramase!");
               $(popupBody).find('button[accesskey="s"]').removeAttr('disabled');
                                }
                                else {
                       $(popupBody).find('button[accesskey="s"]').attr('disabled', 'disabled');
                alert('trebuie sa selectezi o perioada mai mica de' + holidaysLeft + 'de zile');
                                }
                            } else
                            {
                                if (holidaysLeft >= days) {
                                    holidaysLeft = holidaysLeft - days;
                              alert("Mai ai " + holidaysLeft + " zile de concediu ramase!");
                         $(popupBody).find('button[accesskey="s"]').removeAttr('disabled');
                                }
                                else {
                    $(popupBody).find('button[accesskey="s"]').attr('disabled', 'disabled');
          alert('trebuie sa selectezi o perioada mai mica de' + holidaysLeft + 'de zile');
                                }
                            }
                        }
                    });
                }
            }
            ;
        }
        ;
        return false;
    });

    $.ajax({
        type: "GET",
        url: location.protocol + '//' + location.host + "/rest/api/2/myself",
        success: function (response) {
            currentUser = $.parseJSON(response);
        },
        error: function (response) {
            alert("Eroare" + response.result);
        }
    });
    return false;
});
});
 function calcBusinessDays(dDate1, dDate2) { // input given as Date objects
var iWeeks, iDateDiff, iAdjust = 0;
if (dDate2 < dDate1)
    return -1; // error code if dates transposed
var iWeekday1 = dDate1.getDay(); // day of week
var iWeekday2 = dDate2.getDay();
iWeekday1 = (iWeekday1 === 0) ? 7 : iWeekday1; // change Sunday from 0 to 7
iWeekday2 = (iWeekday2 === 0) ? 7 : iWeekday2;
if ((iWeekday1 > 5) && (iWeekday2 > 5))
    iAdjust = 1; // adjustment if both days on weekend
iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;

// calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000);

if (iWeekday1 <= iWeekday2) {
    iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1);
} else {
    iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2);
}

iDateDiff -= iAdjust // take into account both days on weekend

return (iDateDiff + 1); // add 1 because dates are inclusive
}
function parseDate(s) {
var lastSlash = s.lastIndexOf("/");
var years = s.substring(lastSlash + 1);
years = "20" + years;
s = s.replaceAt(lastSlash + 1, years);
var pattern = /(.*?)\/(.*?)\/(.*?)$/;
var result = s.replace(pattern, function (match, p1, p2, p3) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul',
 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    return (months.indexOf(p2) + 1) + "/" + (p1) + "/" + p3;
});
return new Date(result);
}
String.prototype.replaceAt = function (index, character) {
 return this.substr(0, index) + character + this.substr(index + character.length);
};
function propStopped(event) {
var msg = "";
if (event.isPropagationStopped()) {
    msg = "called";
} else {
    msg = "not called";
}
alert(msg);
}

谢谢。

【问题讨论】:

  • 我认为这是一个不好的做法,你的 returnTrue/false 完全没有任何意义
  • @Andreas Furster 我从 jQuery 库中添加了这些。所以,这三个方法已经在点击事件中被调用,但我无法修改它。相反,我想覆盖它们,不适用于我的#tempo-plan-button 元素。谢谢。

标签: javascript jquery jira jira-plugin


【解决方案1】:

你是这个意思吗?

Event.prototype.stopPropagation = function(){ alert('123') }
$('input').on('click',function(e){
  e.stopPropagation();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='button' value='click' />

【讨论】:

  • 是的 @aleha ,类似的东西,但我不想为那个元素被执行。而且我无权访问点击事件脚本。所以我不能修改它。并且所有三个方法都在点击事件中被调用。所以我必须跳过所有三个按钮。
  • 只是使用 Event.prototype 对象覆盖这些方法以清空函数不会有帮助吗? Event.prototype.stopPropagation = function(){ }
  • 不,没有任何改变。
  • 所以这是事件本身的问题,在chrome中根本没有触发焦点,但是触发了mouseout,但是现在如果我使用第二个IE没有看到事件......所以我需要解决方法吗?有什么建议吗?
【解决方案2】:

从问题中,不清楚您控制哪个代码。我在字里行间读到,也许您控制绑定到按钮的 click 事件的函数,但您不控制绑定到 focusout 事件的函数,这就是您需要解决这些调用的原因。

如果您想控制事件的执行并确保您的事件首先发生,则可以使用jQuery.bindUp 之类的方法来执行此操作,只要事件属于同一类型即可。这样一来,您就不必关心其他处理程序是否尝试调用 stopPropagation 等,因为无论如何您的处理程序都会首先执行。

鉴于此,是否可以重构代码,使您控制的逻辑(需要首先发生的部分!)与现有事件处理程序具有相同的事件类型,然后使用 jQuery.bindUp 来确保它在您无法控制的事件处理程序之前执行?如果需要,仍然可以绑定到 click 事件,只要将依赖的业务逻辑移动到 focusout 事件即可。

(如果我的假设不正确,如果您可以更新问题以更详细地描述问题约束,将会很有帮助。)

【讨论】:

  • 谢谢你的回答,但我的问题是chrome没有看到焦点,现在我必须为每个浏览器绑定不同的事件监听器。你有一些例子,如何检测浏览器并根据它绑定不同的事件监听器?
  • @AlexRobert,如果您隔离问题并(例如)组合一个演示确切问题的 jsfiddle,人们会更容易帮助您。这个过程也可能会让你自己发现问题!
  • 我只能把我的代码插入到这个问题中,其他代码在 Atlassian Jira 平台。并且无法访问它。我使用另一个事件监听器解决了这个问题。
猜你喜欢
  • 2023-03-08
  • 1970-01-01
  • 2021-06-26
  • 2011-07-15
  • 1970-01-01
  • 2015-09-09
  • 1970-01-01
  • 2012-12-17
  • 2020-07-28
相关资源
最近更新 更多