gpt4 book ai didi

javascript - 如何在 Chrome 中创建 TouchEvent?

转载 作者:行者123 更新时间:2023-11-29 15:34:27 24 4
gpt4 key购买 nike

W3C specification声明 initTouchEvent 如下:

void initTouchEvent (in DOMString    type,
in boolean canBubble,
in boolean cancelable,
in AbstractView view,
in long detail,
in boolean ctrlKey,
in boolean altKey,
in boolean shiftKey,
in boolean metaKey,
in TouchList touches,
in TouchList targetTouches,
in TouchList changedTouches);

但是,当我在 Chrome 44 中尝试时:

var e = document.createEvent('TouchEvent');
e.initTouchEvent("touchstart", true, true, window, 1,
false, false, false, false, touches, null, null);

其中 touches 是一个有效的 TouchList,这是 Chrome 创建的:

> TouchEvent {}
altKey: false
bubbles: true
cancelBubble: false
cancelable: true
changedTouches: null
charCode: 0
ctrlKey: true
currentTarget: null
defaultPrevented: false
detail: 0
eventPhase: 0
keyCode: 0
layerX: 0
layerY: 0
metaKey: false
pageX: 0
pageY: 0
path: Array[0]
returnValue: true
shiftKey: false
srcElement: null
target: null
targetTouches: null
timeStamp: 1435339572699
touches: null
type: "[object Window]"
view: null
which: 0

仔细查看 type 字段。似乎 Chrome 没有遵循规范,其中第 4 个参数变成了类型而不是第一个参数。

这给我们带来了一个问题,即我如何在 Chrome 中实际创建一个 TouchEvent,因为它不符合规范?

最佳答案

通过查看 Chromium sourceQiita (in Japanese) ,这似乎是它的参数排列方式:

initTouchEvent (TouchList touches,
TouchList targetTouches,
TouchList changedTouches,
String type,
Window view,
number screenX,
number screenY,
number clientX,
number clientY,
boolean ctrlKey,
boolean altKey,
boolean shiftKey,
boolean metaKey);

注意 Chrome 不遵循 W3C 规范。


Chromium 源代码中的相关部分:

TouchEvent.cpp 第 63 行:

void TouchEvent::initTouchEvent(ScriptState* scriptState, TouchList* touches, TouchList* targetTouches,
TouchList* changedTouches, const AtomicString& type,
PassRefPtrWillBeRawPtr<AbstractView> view,
int, int, int, int,
bool ctrlKey, bool altKey, bool shiftKey, bool metaKey)
{
if (dispatched())
return;

if (scriptState->world().isIsolatedWorld())
UIEventWithKeyState::didCreateEventInIsolatedWorld(ctrlKey, altKey, shiftKey, metaKey);

bool cancelable = true;
if (type == EventTypeNames::touchcancel)
cancelable = false;

initUIEvent(type, true, cancelable, view, 0);

m_touches = touches;
m_targetTouches = targetTouches;
m_changedTouches = changedTouches;
m_ctrlKey = ctrlKey;
m_altKey = altKey;
m_shiftKey = shiftKey;
m_metaKey = metaKey;
}

关于javascript - 如何在 Chrome 中创建 TouchEvent?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31079014/

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