Touchstart 事件不适用于原来隐藏的按钮
我正在创建一个 Android 应用程序,有 2 个按钮。 1 次启动和 1 次停止。要开始,一旦单击开始按钮,停止按钮就可见,然后停止按钮将变得可见,而开始按钮将不可见。
我正在尝试在“停止”按钮上获取 touchstart 事件(也可以添加到“开始”按钮,但不是必需的)。但是,由于某种原因,我的下面的代码无法正常工作。有人可以让我知道我错过了什么吗?
背景: - 使用 jquery 隐藏我的按钮 - 带背景图像的按钮
JAVASCRIPT:
var b=document.getElementById('STOP'),start=0;
//Check for touchstart
if('ontouchstart' in document.documentElement)
{
document.getElementById("notouchstart").style.display = "none";
}
//Add a listener that fires at the beginning of each interaction
[b].forEach(function(el){el.addEventListener('touchstart',interact);});
//Add the event handlers for each button
b.addEventListener('touchstart',highlight);
//Functions Store the time when the user initiated an action
function interact(e)
{
start = new Date();
}
//Highlight what the user selected and calculate how long it took the action to occur
function highlight(e)
{
e.preventDefault();
e.currentTarget.className="active";
if(start)
{
alert("test")
}
start = null;
}
HTML 按钮:
<INPUT TYPE="button" style="background:url(images/Start_Btn.png); background-color:transparent; width:150px; height:186px; border:none; cursor:pointer;" id="START" onClick="startBTN();">
<INPUT TYPE="button" style="background:url(images/Stop_Btn.png); background-color:transparent; width:150px; height:186px; border:none; cursor:pointer;" id="STOP">
I am creating an Android app and have 2 buttons. 1 start and 1 stop. To start with the stop button is visible once the start button has been clicked then the stop button becomes visible and start invisible.
I am trying to get a touchstart event on the STOP button (would liek to add to start button as well but not as essential). However, for some reason my below code is not working properly. Could some please let me know what i have missed out.
Background:
- using jquery to hide my buttons
- the buttons with background image
JAVASCRIPT:
var b=document.getElementById('STOP'),start=0;
//Check for touchstart
if('ontouchstart' in document.documentElement)
{
document.getElementById("notouchstart").style.display = "none";
}
//Add a listener that fires at the beginning of each interaction
[b].forEach(function(el){el.addEventListener('touchstart',interact);});
//Add the event handlers for each button
b.addEventListener('touchstart',highlight);
//Functions Store the time when the user initiated an action
function interact(e)
{
start = new Date();
}
//Highlight what the user selected and calculate how long it took the action to occur
function highlight(e)
{
e.preventDefault();
e.currentTarget.className="active";
if(start)
{
alert("test")
}
start = null;
}
HTML BUTTON:
<INPUT TYPE="button" style="background:url(images/Start_Btn.png); background-color:transparent; width:150px; height:186px; border:none; cursor:pointer;" id="START" onClick="startBTN();">
<INPUT TYPE="button" style="background:url(images/Stop_Btn.png); background-color:transparent; width:150px; height:186px; border:none; cursor:pointer;" id="STOP">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在同一元素上有两个
touchstart
事件。因此,它同时执行interact
和highlight
。这是故意的吗?此外,您没有将event
传递到highlight(e)
函数中。您需要将其包装在传递它的匿名函数中:另外,不要忘记将
false
添加到您的addEventListener
声明中。编辑:我们不希望同一个元素有两个
touchstart
事件监听器。我们需要修改forEach
语句以及highlight
函数。You have two
touchstart
events on the same elements. Therefore, it's performinginteract
andhighlight
at the same time. Is that intentional? Also, you're not passingevent
into yourhighlight(e)
function. You need to wrap it in an anonymous function that passes it:Also, don't forget to add
false
to youraddEventListener
declaration.EDIT: We don't want to have two event listeners for
touchstart
to the same element. We need to modify theforEach
statement as well as thehighlight
function.我已经解决了我的问题,我试图变得太聪明。我只需要 onload 函数中的两行代码:
调用函数 onload:
希望这对某人有帮助
I have solved my issue, I was trying to get too clever about it. I simply need 2 lines of code within the onload function:
To Call Function onload:
Hope this helps someone