[英]mousedown event using javascript
我正在處理的代碼是,一旦我單擊一個按鈕,兩個電動機就會連續旋轉,直到我按下另一個按鈕才能停止。 我希望能夠按住一個按鈕來旋轉電動機,但是一旦松開該按鈕,電動機就會停止運轉。
我的remoteControl.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>
我正在嘗試使用JavaScript:
<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。 如果那是可行的,唯一的問題是您不知道要使用該功能做什么才能用狀態更新數據庫(我想有些控制器可以檢查數據庫的狀態並更新GPIO州)。
您需要調用文件remoteControl.php(使用GET參數來更新數據庫)。 這可以使用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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.