繁体   English   中英

如何在React中访问Synthetic事件的属性?

[英]How to access properties of Synthetic event in React?

例如,在合成touchEvents中:

onTouchCancel onTouchEnd onTouchMove onTouchStart

文档列出了这些属性:

boolean altKey
DOMTouchList changedTouches
boolean ctrlKey
boolean getModifierState(key)
boolean metaKey
boolean shiftKey
DOMTouchList targetTouches
DOMTouchList touches

如何严格使用JavaScript DOMTouchList targetTouches jQuery访问这些属性(例如DOMTouchList targetTouches

toggle(e) {
  console.log('e.ctrlKey', e.ctrlKey);
}

对于targetTouches,您可以执行以下操作

function touches_in_target(ev) {
  // Return true if all of the touches are within the target element;
  // otherwise return false.
  return (ev.touches.length == ev.targetTouches.length ? true : 
  false);
}

您可以从传递给事件属性的回调函数访问事件的那些属性。 例如:

onTouchStart={event => { /* accessing metaKey */ console.log(event.metakey) }}

以下是一个实时示例。 我没有使用touchEvents,因为那仅适用于移动设备。 我正在通过onClick事件访问pageX属性。

 const Test = () => <div style={{height: 200, width: 200, backgroundColor: 'red'}} onClick={e => console.log(e.pageX)} /> ReactDOM.render(<Test />, document.getElementById('app')); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="app"></div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM