使用javascript的Mousedown事件
mousedown event using javascript
我所做的代码是,一旦我点击一个按钮,两个电机将连续旋转,直到我点击一个不同的按钮停止。我希望能够按住按钮来旋转马达,但是一旦松开按钮,马达就会停止运转。
remoteccontrol .php文件的一部分:
$action = $_GET['action'];
$pin = mysql_real_escape_string($_GET['pin']);
if ($action == "forward"){
$setting = "1";
mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='17';");
mysql_query("UPDATE pinStatus SET pinStatus='$setting' WHERE pinNumber='22';");
mysql_close();
header('Location: remoteControl.php'); ..........
........
<form action="remoteControl.php" method="get">
<input id="forward" type="image" src="uparrow.jpg" IMG STYLE="position:absolute; TOP:150px; LEFT:170px; WIDTH:50px; HEIGHT:50px;">
<input type=hidden name="action" value="forward">
</form>
<head>
<script type="text/javascript">
function OnButtonDown (button) {
"can't figure out what to put";
}
function OnButtonUp (button) {
"can't figure out what to put";
}
function Init () {
var button = document.getElementById ("forward");
if (button.addEventListener) { // all browsers except IE before version 9
button.addEventListener ("mousedown", function () {OnButtonDown (button)}, false);
button.addEventListener ("mouseup", function () {OnButtonUp (button)}, false);
}
else {
if (button.attachEvent) { // IE before version 9
button.attachEvent ("onmousedown", function () {OnButtonDown (button)});
button.attachEvent ("onmouseup", function () {OnButtonUp (button)});
}
}
}
</script>
</head>
<body onload="Init ()">
当鼠标按下时调用OnButtonDown当鼠标按上时调用OnButtonUp。如果这是工作,唯一的问题是,你不知道该怎么做的功能,以更新你的DB的状态(我想有某种类型的控制器检查DB的状态和更新GPIO状态)。
您需要调用文件remoteccontrol .php(它接受一个GET参数来更新DB)。这可以使用jquery中的ajax函数来完成。
<head>
<!-- First we include jquery library. In this case from Google CDN -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
function OnButtonDown (button) {
//We pass the parameter forward to remoteControl.php
$.ajax({
data: action=forward,
url: 'remoteControl.php',
type: 'get',
success: function (response) {
}
});
}
function OnButtonUp (button) {
//Here the same as in OnButtonDown but passing another parameter
}
function Init () {
var button = document.getElementById ("forward");
if (button.addEventListener) { // all browsers except IE before version 9
button.addEventListener ("mousedown", function () {OnButtonDown (button)}, false);
button.addEventListener ("mouseup", function () {OnButtonUp (button)}, false);
}
else {
if (button.attachEvent) { // IE before version 9
button.attachEvent ("onmousedown", function () {OnButtonDown (button)});
button.attachEvent ("onmouseup", function () {OnButtonUp (button)});
}
}
}
</script>
相关文章:
- 使用谷歌图表将mousedown事件注册为click
- 捕获元素jquery外部的mousedown事件
- ExtJS 4按钮上的mousedown/mouseup事件
- 如何在JavaScript中测量交互和mousedown事件之间的延迟
- 用标准javascript而不是jquery中的mousedown、mousemove和mouseup触摸事件句柄
- mousedown事件未在平板电脑/移动设备上启动,HTML5 Canvas
- jQuery mousedown() 函数无法在拖动事件上触发
- $('option').mousedown 事件在少数情况下不起作用
- 如何在mousedown上检测鼠标右键事件
- mousedown事件完成后销毁引导程序弹出窗口
- 是mousedown事件固有地比mouseup事件慢
- 谷歌地图矩形mouseDown和mouseUp事件无法在边界触发
- RaphaelJS中的mousedown事件不起作用
- iPhone文档mousedown事件未启动
- 如何使mousedown事件防止单击效果,并且只影响按住的单击
- Jquery.mousenter()事件比.click()或.mousedown()事件快得多
- 为什么在javascript中无法识别mousedown事件(带THREE)
- 如果单击了滚动条,则取消mousedown事件
- 如何检查mousedown事件是'javascript中滚动条上的t
- 在javascript / jquery中取消事件mousedown