简体   繁体   English

IE> = 8的原型Event.StopPropagation

[英]Prototype Event.StopPropagation for IE >= 8

I understand the proper way to handle event.stopPropagation for IE is 我理解为IE处理event.stopPropagation的正确方法是

if(event.stopPropagation) {
    event.stopPropagation();
} else {
    event.returnValue = false;
}

But is it possible to prototype Event so that I don't have to do the check each and everytime I use stopPropagation? 但是有可能对Event进行原型设计,这样我每次使用stopPropagation时都不需要进行检查吗?

This question seemed helpful: JavaScript Event prototype in IE8 however I don't quite understand the accepted answer and how it is a prototype that can essentially be set and forget. 这个问题似乎很有帮助: IE8中的JavaScript事件原型然而我并不完全理解接受的答案以及它如何成为一个基本上可以设置和忘记的原型。

Probably this: 可能是这样的:

Event = Event || window.Event;
Event.prototype.stopPropagation = Event.prototype.stopPropagation || function() {
    this.cancelBubble = true;
}

returnValue = false is an analogue for preventDefault: returnValue = false是preventDefault的类似物:

Event.prototype.preventDefault = Event.prototype.preventDefault || function () {
    this.returnValue = false;
}

If you're doing your own event handling in plain javascript, then you probably already have a cross browser routine for setting event handlers. 如果你在普通的javascript中进行自己的事件处理,那么你可能已经有了一个用于设置事件处理程序的跨浏览器例程。 You can put the abstraction in that function. 您可以将抽象放在该函数中。 Here's one that I use that mimics the jQuery functionality (if the event handler returns false , then both stopPropagation() and preventDefault() are triggered. You can obviously modify it however you want it to behave: 这是我使用的模仿jQuery功能的一个(如果事件处理程序返回false ,则触发stopPropagation()preventDefault() 。显然你可以修改它,但是你希望它表现如下:

// refined add event cross browser
function addEvent(elem, event, fn) {
    // allow the passing of an element id string instead of the DOM elem
    if (typeof elem === "string") {
        elem = document.getElementById(elem);
    }

    function listenHandler(e) {
        var ret = fn.apply(this, arguments);
        if (ret === false) {
            e.stopPropagation();
            e.preventDefault();
        }
        return(ret);
    }

    function attachHandler() {
        // normalize the target of the event
        window.event.target = window.event.srcElement;
        // make sure the event is passed to the fn also so that works the same too
        // set the this pointer same as addEventListener when fn is called
        var ret = fn.call(elem, window.event);   
        // support an optional return false to be cancel propagation and prevent default handling
        // like jQuery does
        if (ret === false) {
            window.event.returnValue = false;
            window.event.cancelBubble = true;
        }
        return(ret);
    }

    if (elem.addEventListener) {
        elem.addEventListener(event, listenHandler, false);
    } else {
        elem.attachEvent("on" + event, attachHandler);
    }
}

Or, you can just make a utility function like this: 或者,你可以像这样制作一个实用函数:

function stopPropagation(e) {
    if(e.stopPropagation) {
        e.stopPropagation();
    } else {
        e.returnValue = false;
    }    
}

And just call that function instead of operating on the event in each function. 只需调用该函数,而不是在每个函数中对事件进行操作。

We can use this alone: event.cancelBubble = true . 我们可以单独使用它: event.cancelBubble = true

Tested working in all browsers. 测试在所有浏览器中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM