Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript或react检查touchevent是否存在?_Javascript_Reactjs - Fatal编程技术网

如何使用javascript或react检查touchevent是否存在?

如何使用javascript或react检查touchevent是否存在?,javascript,reactjs,Javascript,Reactjs,我想消除safari浏览器上的错误“referenceError:找不到变量TouchEvent”。我的代码如下: function copy_mouse_event(event) { const event_init = { // EventInit bubbles: event.bubbles, eventPhase: event.eventPhase, // UIEventInit detail: event.detail, vie

我想消除safari浏览器上的错误“referenceError:找不到变量TouchEvent”。我的代码如下:

function copy_mouse_event(event) {
    const event_init = {
    // EventInit
    bubbles: event.bubbles,
    eventPhase: event.eventPhase,

    // UIEventInit
    detail: event.detail,
    view: event.view,

    // EventModifierInit
    altKey: event.altKey,
    ctrlKey: event.ctrlKey
  };

  // initialise MouseEvent data which is shared by wheel, mouse and pointer events
  let mouse_event_init = {};
  if (event instanceof MouseEvent) {
      mouse_event_init = {
          ...event_init,
          button: event.button,
          buttons: event.buttons,
          clientX: event.clientX,
          clientY: event.clientY,
          relatedTarget: event.relatedTarget,
          screenX: event.screenX,
          screenY: event.screenY
      };

      if (event instanceof WheelEvent) {
          const wheel_event_init = {
              ...mouse_event_init,
              deltaMode: event.deltaMode,
              deltaX: event.deltaX,
              deltaY: event.deltaY,
              deltaZ: event.deltaZ,
              wheelDelta: event.wheelDelta,
              wheelDeltaX: event.wheelDeltaX,
              wheelDeltaY: event.wheelDeltaY
          };
          return new WheelEvent(event.type, wheel_event_init);
      }
  }

  // try the modern pointer event first, then mouse and touch events
  if (event instanceof PointerEvent) {
      const pointer_event_init = {
          ...mouse_event_init,
          pointerId: event.pointerId,
          width: event.width,
          height: event.height,
      };
      return new PointerEvent(event.type, pointer_event_init);
  } else if (event instanceof MouseEvent) {
      return new MouseEvent(event.type, mouse_event_init);
  } else if (event instanceof TouchEvent) {
     //error occurs here only on safari
     const touch_event_init = {
         ...event_init,
         changedTouches: event.changedTouches,
         targetTouches: event.targetTouches,
         touches: event.touches
     };
     return new TouchEvent(event.type, touch_event_init);
 }
}

因为safari浏览器不支持触摸事件,所以我想通过检查触摸事件是否存在来修复。如何检查touchevent是否存在。有人能帮我吗。谢谢。

您只需检查以下各项即可:


您只需检查以下各项即可:


要检查是否存在
TouchEvent
,您可以执行以下操作:

if ('TouchEvent' in window) {
   ...
}
只需检查
TouchEvent
,如在
中,如果(TouchEvent…
不起作用,如未定义,则会出现运行时错误。有效的方法是:

 if (window.TouchEvent) {
     ...
 }
编辑:如果要使用
typeof
,可以这样做:

if (typeof TouchEvent !== 'undefined') {
    ...
}
使用您喜欢的方法,因为这两种方法都有效


希望这有帮助。

要检查是否存在
TouchEvent
,您可以执行以下操作:

if ('TouchEvent' in window) {
   ...
}
只需检查
TouchEvent
,如在
中,如果(TouchEvent…
不起作用,如未定义,则会出现运行时错误。有效的方法是:

 if (window.TouchEvent) {
     ...
 }
编辑:如果要使用
typeof
,可以这样做:

if (typeof TouchEvent !== 'undefined') {
    ...
}
使用您喜欢的方法,因为这两种方法都有效


希望这有帮助。

我可以使用typeof运算符检查是否定义了touchevent以及如何执行?感谢我使用typeof运算符检查是否定义了touchevent以及如何执行?谢谢