我理解为 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/