React Synthetic Event区分左击事件和右击事件 [英] React Synthetic Event distinguish Left and Right click events
问题描述
我试图区分OnClick函数中的左键单击和右键单击。但是,
I am trying to distinguish between left and right clicks in an OnClick function. But,
var r = React.createClass({
handleClick : function(e){
//left click
if(e.which==1){
//Do something
}
},
render : function(){
return <p onClick={this.handleClick}>Something </p>
}
});
显示未为综合事件。我如何在这里区分左键和右键?
Turns out e.which is undefined for Synthetic Events. How can I distinguish between left and right clicks here?
推荐答案
您也可以执行类似的操作。同时拥有onClick和onContextMenu处理程序
You can do something like this too. Have both onClick and onContextMenu handlers
return <p onClick={this.handleClick} onContextMenu={this.handleClick}>Something </p>
您可以检查 nativeEvent
作为其他答案建议或检查类型
。 (此外,如果单击鼠标右键,则阻止默认设置。)
You can either check for nativeEvent
as the other answer suggests or check for type
. (Also, prevent default if it is a right click.)
使用 type
handleClick: function(e) {
if (e.type === 'click') {
console.log('Left click');
} else if (e.type === 'contextmenu') {
console.log('Right click');
}
}
使用 nativeEvent
handleClick: function(e) {
if (e.nativeEvent.which === 1) {
console.log('Left click');
} else if (e.nativeEvent.which === 3) {
console.log('Right click');
}
}
这里是一个演示 http://jsbin.com/seyeliv/edit?html,输出
Here is a demo http://jsbin.com/seyeliv/edit?html,output
这篇关于React Synthetic Event区分左击事件和右击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!