我正在尝试向Event原型添加一个方法.为了调用/设置,preventDefault()或者在IE中说话returnValue = false和-if desired- stopPropagation()/ cancelBubble = true;.我认为下面的代码已经足够了.
Event = Event || window.Event;
//^^ makes the fiddle work on IE8 ^^
if(!(Event.prototype.stopEvent))
{
Event.prototype.stopEvent = function(propagate)
{
"use strict";
propagate = (propagate ? true : false);
if (this.preventDefault)
{
this.preventDefault();
if (propagate === false)
{
this.stopPropagation();
}
}
else
{
this.returnValue = false;
this.cancelBubble = !propagate;
}
return this;
};
}
Run Code Online (Sandbox Code Playgroud)
这看起来很有效,你可以在这里看到.这个小提琴OK在IE8,firefox和chrome中显示.虽然,当我将其添加到我的脚本时,IE8在第一行中断,说"事件未定义".离开"use strict";没有任何区别.
不情愿地,我也尝试了这个:
if (typeof …Run Code Online (Sandbox Code Playgroud)