是否可以在同一个按钮上使用 mousedown 和 mouseup 事件? [英] Is it possible to use the mousedown and mouseup event on the same button?

查看:53
本文介绍了是否可以在同一个按钮上使用 mousedown 和 mouseup 事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该项目涉及通过 LAN 控制玩具车.

This project involves controlling a toy car over LAN.

在我的 html 页面上,有 5 个按钮";上、下、左、右、停止.我的代码是这样工作的:假设用户点击up",一个值被发送到一个 php 页面进行处理,处理后的字符串被打印在另一个 php 页面上供 arduino 读取.

On my html page, there are 5 "buttons";up, down, left, right, stop. My code works like this: Say the user clicks on "up", a value gets sent to a php page for processing and the processed string gets printed on another php page for an arduino to read.

要让汽车停下,用户必须点击停止.

For the car to stop, the user has to click on stop.

<html>
<head>
</head>

<form name="motorform" action="motorboardprocess.php" method="post">
<tr><p align='center'><font face="BN machine" size="6" color="royalblue">Motor Shield</font></tr>
<tr>
    <input id="forward" type="image" src="up.png" name="forward">
</tr> 
<tr>
    <input id="left" type="image" src="left.png"  name="left">
    <input id="stop"type="image" src="stop.png"  name="stop">
    <input id="right" type="image" src="right.png" name="right">
</tr>
<tr>      
    <input id="backward" type="image" src="down.png" name="backward">
</tr>   
</form>
</body>
</html>

和所说的 php...

and the said php...

<?php

if (isset($_POST['forward_x'], $_POST['forward_y'])){
    $myFile = "mtboardcom.php";
    $fh = fopen($myFile, 'w') or die("can't open file");    
    $stringData = "<?php\n";
    fwrite($fh, $stringData);
    $stringData = "echo ".'"<forward>"'."\n";
            fwrite($fh, $stringData);
    $stringData = "?>\n";
            fwrite($fh, $stringData);
    fclose($fh);        
}

else if (isset($_POST['left_x'], $_POST['left_y'])){
    $myFile = "mtboardcom.php";
    $fh = fopen($myFile, 'w') or die("can't open file");    
    $stringData = "<?php\n";
    fwrite($fh, $stringData);
    $stringData = "echo ".'"<left>"'."\n";
            fwrite($fh, $stringData);
    $stringData = "?>\n";
            fwrite($fh, $stringData);
    fclose($fh);        
}
//and so on.....

arduino 上写着:

the arduino reads:

<?php
    echo "<forward>"
?>

所以问题是,我如何利用 jquery 的 mousedownmouseup 事件删除停止按钮".

So the question is, how do I make use of the mousedown and mouseup events from jquery to remove the stop 'button'.

或者其他场景:用户按住按钮移动汽车,松开按钮停止汽车.

Or another scenario: the user holds down the button to move the car and releases the button to stop the car.

mousedown 上,arduino 读取字符串 'forward'.

On mousedown, a string 'forward' is read by the arduino.

mouseup 在同一个按钮上,字符串 'stop' 被 arduino 读取.

mouseup on the same button, string 'stop' is read by the arduino.

推荐答案

当然,您可以在 Javascript 中订阅两个不同的事件.只需挂钩事件:

Of course you can subscribe to two different events in Javascript. just hook into the events:

var field = document.getElementsById('#idName');

field.onmousedown = function(){

}

field.onmouseup = function(){

}

或者对于 jquery:

Or for jquery:

var field = $('#idName');

field.mousedown(function(){

}).mouseup(function(){

});

这篇关于是否可以在同一个按钮上使用 mousedown 和 mouseup 事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆