gpt4 book ai didi

jquery - 检测两个不同元素的触摸

转载 作者:行者123 更新时间:2023-12-01 06:03:25 25 4
gpt4 key购买 nike

使用phonegap、jquery。是否可以同时检测两个不同元素上的触摸?

我正在创建一些需要按下两个图像(左按钮和右按钮)的东西在触发另一个事件之前的同一时间。

以下不是详尽的代码列表,但应该提供对我试图实现的目标的深入了解

<head>

<script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>
<script type="text/javascript" charset="utf-8" src="jq.js"></script>
<script type="text/javascript" src="jquery.stopwatch.js"></script>

<script>
var w = new Stopwatch(updatedisplay, 50);
var onoff = false;

$("#button-left, #button-right").unbind("touch",begin);

function begin() {
onoff = true;
w.start();
}

function end() {
w.stop();
onoff = false;
}

function updatedisplay(watch) {
var mills = parseInt(watch.getElapsed().milliseconds/10);
if(mills<10) {
millis = '0'+mills;
} else {
millis = mills;
}
document.getElementById('counterValue').innerHTML = watch.toString() + "." + millis;
}

function reset() {
w.stop();
onoff = false;
document.getElementById('counterValue').innerHTML = "00:00:00.00";
}
</script>
</head>

<body onload="onBodyLoad()">
<div id="counterValue">
00:00:00.00
</div>
<div id="button-left"></div>
<div id="button-right"></div>
</body>

:::::创建了此修复程序:::::有点丑陋,但效果很好:::::

我找到了另一种方法,现在这可能有点难看..但效果很好......

    $(document).ready(function() {                          
document.getElementById('button-left').addEventListener('touchstart', nofunc(e), false);
document.getElementById('button-right').addEventListener('touchstart', nofunc(e), false);
});

function vala(val) {
av = val;
checkvals();
}
function valb(val) {
bv = val;
checkvals();
}
function checkvals() {
if(av == 1 && bv == 1) {
if(onoff == false) {
begin();
} else {
end();
}
}
}

<div id="button-left" ontouchstart="vala(1)" ontouchend="vala(0)">&nbsp;</div>
<div id="button-right" ontouchstart="valb(1)" ontouchend="valb(0)">&nbsp;</div>

最佳答案

你的意思是unbind,因为detach是其他东西,它会从DOM中删除元素,要取消绑定(bind)事件,有unbind

$("#btn1, #btn2").unbind("touch");

更新

更改包含 js 文件的顺序,首先必须加载 jquery.js,然后加载使用它的其他库,其次我已从 中删除了 var wonoff 使它们成为全局范围,第三,我删除了 onbodyload 而是将触摸事件绑定(bind)在就绪处理程序中,如下所示,尝试

<head>

<script type="text/javascript" charset="utf-8" src="jq.js"></script>
<script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>
<script type="text/javascript" src="jquery.stopwatch.js"></script>

<script>
w = new Stopwatch(updatedisplay, 50);
onoff = false;

function begin() {
onoff = true;
w.start();
}

function end() {
w.stop();
onoff = false;
}

function updatedisplay(watch) {
var mills = parseInt(watch.getElapsed().milliseconds/10);
if(mills<10) {
millis = '0'+mills;
} else {
millis = mills;
}
document.getElementById('counterValue').innerHTML = watch.toString() + "." + millis;
}

function reset() {
w.stop();
onoff = false;
document.getElementById('counterValue').innerHTML = "00:00:00.00";
}

$(document).ready(function(){
$("#button-left, #button-right").bind("touch",begin);
});
</script>
</head>

<body>
<div id="counterValue">
00:00:00.00
</div>
<div id="button-left"></div>
<div id="button-right"></div>
</body>

关于jquery - 检测两个不同元素的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8960263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com