[英]Can't trigger self-made Event.prototype
我有这个原型功能 - >
Event.prototype.eventPreventDefault = function () {
this.preventDefault ? this.preventDefault() : this.returnValue = false;
}
我通过这样做来调用代码 - >
$element.click(function (evt) {
evt.eventPreventDefault();
}
但我明白了
未捕获的TypeError:undefined不是函数
我做错了什么? 谢谢!
由jQuery创建的事件对象(并传递给事件处理程序)不是本机事件的子项( “首选组合而不是继承”原则 )。 他们有自己的一组方法 (在jQuery.Event.prototype
上jQuery.Event.prototype
):
jQuery.Event.prototype = {
constructor: jQuery.Event,
isDefaultPrevented: returnFalse,
isPropagationStopped: returnFalse,
isImmediatePropagationStopped: returnFalse,
preventDefault: function() {
var e = this.originalEvent;
this.isDefaultPrevented = returnTrue;
if ( e && e.preventDefault ) {
e.preventDefault();
}
},
stopPropagation: function() {
var e = this.originalEvent;
this.isPropagationStopped = returnTrue;
if ( e && e.stopPropagation ) {
e.stopPropagation();
}
},
// ... and so on.
};
如您所见,这些方法通常调用原始Event的同名方法(存储在jQuery.Event实例的originalEvent
属性中)。 关键是, jQuery.Event
在其继承链中没有Event.prototype
,因此对后者的任何修改都注定对前者没有影响。
底线:扩展传递给jQuery事件处理程序的事件,扩展jQuery.Event.prototype
而不是Event.prototype
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.