gpt4 book ai didi

javascript - IE >= 8 的原型(prototype) Event.StopPropagation

转载 作者:数据小太阳 更新时间:2023-10-29 05:50:15 26 4
gpt4 key购买 nike

我理解为 IE 处理 event.stopPropagation 的正确方法是

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

但是是否可以对 Event 进行原型(prototype)设计,这样我就不必在每次使用 stopPropagation 时都进行检查?

这个问题似乎很有帮助:JavaScript Event prototype in IE8但是我不太理解公认的答案,也不太理解它是一个基本上可以设置并忘记的原型(prototype)。

最佳答案

大概是这样的:

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

returnValue = false 是 preventDefault 的模拟:

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

关于javascript - IE >= 8 的原型(prototype) Event.StopPropagation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17102300/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com