JavaScript/react/onClick:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
创建页面,内容为“== 引言 == == 示例 == <syntaxhighlight lang="javascript"> import React from "react"; import ReactDOM from "react-dom"; import Button from './Button' const handl…”
 
Kangkk​(留言 | 贡献)
第1行: 第1行:
== 引言 ==
== 引言 ==
== e.target ==
在事件处理的时候,如
{{leftrightflex|<syntaxhighlight lang="javascript">
const selectHandle = (e: any) => console.log(e);
</syntaxhighlight>|{{consoleout|<pre>_dispatchInstances: null
_dispatchListeners: null
_targetInst: null
altKey:
bubbles:
button:
buttons:
cancelable:
clientX:
clientY:
ctrlKey:
currentTarget:
defaultPrevented:
detail:
dispatchConfig: null
eventPhase:
getModifierState:
isDefaultPrevented:
isPropagationStopped:
isTrusted:
metaKey:
movementX:
movementY:
nativeEvent:
pageX:
pageY:
preventDefault:
relatedTarget:
screenX:
screenY:
shiftKey:
stopPropagation:
target:
timeStamp:
type:
view:
</pre>}}}}
遇到 e.target 丢失,这时可如下处理
{{leftrightflex|<syntaxhighlight lang="javascript">
const selectHandle = (e: any) => {
  e.persist();
  console.log(e);
}
</syntaxhighlight>|{{consoleout|<pre>_dispatchInstances: null
_dispatchListeners: null
_targetInst: FiberNode {tag: 5, key: null, elementType: "span", type: "span", stateNode: <span class="simpleSelect-cmJcLbvV27">, …}
altKey: false
bubbles: true
button: 0
buttons: 0
cancelable: true
clientX: 135
clientY: 478
ctrlKey: false
currentTarget: null
defaultPrevented: false
detail: 1
dispatchConfig: {phasedRegistrationNames: {bubbled: "onClick", captured: "onClickCapture"}, dependencies: ["click"], eventPriority: 0}
eventPhase: 3
getModifierState: function(keyArg)
isDefaultPrevented: function()
isPersistent: function()
isPropagationStopped: function()
isTrusted: true
metaKey: false
movementX: 0
movementY: 0
nativeEvent: MouseEvent {isTrusted: true, screenX: 1183, screenY: 666, clientX: 135, clientY: 478, …}
pageX: 135
pageY: 478
relatedTarget: null
screenX: 1183
screenY: 666
shiftKey: false
target: <span class="simpleSelect-cmJcLbvV27">
timeStamp: 4955
type: "click"
view: Window {document: #document, window: Window, Infinity: Infinity, NaN: NaN, undefined: undefined, …}
</pre>}}}}
== 示例 ==
== 示例 ==
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">