gpt4 book ai didi

jquery - jQuery 中的 $.event.fixHooks 是什么?

转载 作者:行者123 更新时间:2023-12-01 04:08:59 25 4
gpt4 key购买 nike

我在 jquery.mousewheel.js 中找到了这段代码

我的问题是$.event.fixHooks是什么?在哪里可以找到它的文档?我找不到任何有用的文档来解释用法。

var toFix  = ['wheel', 'mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'],
toBind = ( 'onwheel' in document || document.documentMode >= 9 ) ?
['wheel'] : ['mousewheel', 'DomMouseScroll', 'MozMousePixelScroll'],
slice = Array.prototype.slice,
nullLowestDeltaTimeout, lowestDelta;

if ( $.event.fixHooks ) {
for ( var i = toFix.length; i; ) {
$.event.fixHooks[ toFix[--i] ] = $.event.mouseHooks;
}
}

最佳答案

关于Event.fixHooks:

The fixHooks interface provides a per-event-type way to extend or normalize the event object that jQuery creates when it processes a native browser event. A fixHooks entry is an object that has two properties, each being optional:

props: Array: Strings representing properties that should be copied from the browser's event object to the jQuery event object. If omitted, no additional properties are copied beyond the standard ones that jQuery copies and normalizes (e.g. event.target and event.relatedTarget).

filter: Function( event, originalEvent ): jQuery calls this function after it constructs the jQuery.Event object, copies standard properties from jQuery.event.props, and copies the fixHooks-specific props (if any) specified above. The function can create new properties on the event object or modify existing ones. The second argument is the browser's native event object, which is also available in event.originalEvent.

Note that for all events, the browser's native event object is available in event.originalEvent; if the jQuery event handler examines the properties there instead of jQuery's normalized event object, there is no need to create a fixHooks entry to copy or modify the properties.

For example, to set a hook for the "drop" event that copies the dataTransfer property, assign an object to jQuery.event.fixHooks.drop:

jQuery.event.fixHooks.drop = {
props: [ "dataTransfer" ]
};

Since fixHooks is an advanced feature and rarely used externally, jQuery does not include code or interfaces to deal with conflict resolution. If there is a chance that some other code may be assigning fixHooks to the same events, the code should check for an existing hook and take appropriate measures. A simple solution might look like this:

if ( jQuery.event.fixHooks.drop ) {
throw new Error( "Someone else took the jQuery.event.fixHooks.drop hook!" );
}

jQuery.event.fixHooks.drop = {
props: [ "dataTransfer" ]
};

关于此代码

if ( $.event.fixHooks ) {
for ( var i = toFix.length; i; ) {
$.event.fixHooks[ toFix[--i] ] = $.event.mouseHooks;
}
}

基本上,这是扩展/规范化 'wheel'、'mousewheel'、'DOMMouseScroll'、'MozMousePixelScroll' 事件。将它们的值设置为 $.event.mouseHooks。现在您可能会问这个 event.mouseHooks 是做什么的?

jQuery.event.mouseHooks 是

  • 用于将一些属性从原始MouseEvent转移到jQuery 事件对象。
  • 跨浏览器标准化 event.which(使用了哪个鼠标按钮),因为 event.button 未标准化。
  • 计算 pageXpageYclientXclientY 以及其他内容(如果缺失)将它们设置为 jQuery 事件对象

希望对你有帮助

引用文献:

关于jquery - jQuery 中的 $.event.fixHooks 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23464864/

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