如何仅对鼠标左键单击使用监听事件?
How to use listen to event only for mouse left click?
默认情况下,Phaser 将所有鼠标点击(也包括中间和右侧)视为点击事件。我只想在单击鼠标左键时执行一个函数,而不是在 right/middle 单击时执行。有什么办法吗?
下面link是Phaser的例子,其中right/middle点击也执行:https://phaser.io/examples/v2/basics/02-click-on-an-image.
game.input.activePointer.leftButton.isDown
<div onmousedown="WhichButton(event)">Click this text with one of your mouse buttons to return a number.
<p>
0 = The left mouse button<br>
1 = The middle mouse button<br>
2 = The right mouse button
</p>
</div>
<script>
function WhichButton(event) {
alert("You pressed button: " + event.button)
}
</script>
这里0表示左键点击所以你可以这样写条件:
function WhichButton(event) {
if(event.button==0) {
alert('left click');
}
}
希望对您有所帮助。
建议:
在你想要的元素的 mouseup 事件上绑定一个函数,并计算事件的 "button" 属性。该字段的值将指示是左击还是右击
<!DOCTYPE html>
<html>
<body>
<script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
crossorigin="anonymous"></script>
click me :
<a id="test" href="#">Try it</a>
<script>
$("#test").on("mouseup", function(e) { myFunction(e);});
function myFunction(e) {
if(e.button === 0)
alert('left click');
if(e.button === 2)
alert('right click');
}
</script>
</body>
</html>
默认情况下,Phaser 将所有鼠标点击(也包括中间和右侧)视为点击事件。我只想在单击鼠标左键时执行一个函数,而不是在 right/middle 单击时执行。有什么办法吗?
下面link是Phaser的例子,其中right/middle点击也执行:https://phaser.io/examples/v2/basics/02-click-on-an-image.
game.input.activePointer.leftButton.isDown
<div onmousedown="WhichButton(event)">Click this text with one of your mouse buttons to return a number.
<p>
0 = The left mouse button<br>
1 = The middle mouse button<br>
2 = The right mouse button
</p>
</div>
<script>
function WhichButton(event) {
alert("You pressed button: " + event.button)
}
</script>
这里0表示左键点击所以你可以这样写条件:
function WhichButton(event) {
if(event.button==0) {
alert('left click');
}
}
希望对您有所帮助。
建议:
在你想要的元素的 mouseup 事件上绑定一个函数,并计算事件的 "button" 属性。该字段的值将指示是左击还是右击
<!DOCTYPE html>
<html>
<body>
<script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
crossorigin="anonymous"></script>
click me :
<a id="test" href="#">Try it</a>
<script>
$("#test").on("mouseup", function(e) { myFunction(e);});
function myFunction(e) {
if(e.button === 0)
alert('left click');
if(e.button === 2)
alert('right click');
}
</script>
</body>
</html>