最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - touchend handler fires twice - Stack Overflow

programmeradmin2浏览0评论

On a webapp on iOS, I have a bunch of buttons that respond only to touchend (as a shortcut to the click delay in mobile safari). When I stick an alert in the handler, then a subsequent tap of any other button on the page fires this original handler even though they have their own handlers. Here's some sample code that illustrates the problem:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="default" />

<script type="text/javascript">

function clickAlert(evt) {
    alert('btn clicked');
}

function clickData(evt) {
    var div;

    div = document.getElementById('data');
    div.innerHTML += 'btn2 click: ' + (new Date().getTime()) + '<br/>';
}

function loadHandler() {
    var btn;

    btn = document.getElementById('btn-click-alert');
    btn.addEventListener('touchend', clickAlert, false);
    btn = document.getElementById('btn-noclick');
    btn.addEventListener('touchend', clickData, false);
}

window.addEventListener('load', loadHandler, false);
</script>

</head>
<body>

<button id="btn-click-alert">Click to alert</button>
<button id="btn-noclick">No alert here</button>
<div id="data"> </div>
</body>
</html>

Can someone help?

Thanks!

On a webapp on iOS, I have a bunch of buttons that respond only to touchend (as a shortcut to the click delay in mobile safari). When I stick an alert in the handler, then a subsequent tap of any other button on the page fires this original handler even though they have their own handlers. Here's some sample code that illustrates the problem:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=no;" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="default" />

<script type="text/javascript">

function clickAlert(evt) {
    alert('btn clicked');
}

function clickData(evt) {
    var div;

    div = document.getElementById('data');
    div.innerHTML += 'btn2 click: ' + (new Date().getTime()) + '<br/>';
}

function loadHandler() {
    var btn;

    btn = document.getElementById('btn-click-alert');
    btn.addEventListener('touchend', clickAlert, false);
    btn = document.getElementById('btn-noclick');
    btn.addEventListener('touchend', clickData, false);
}

window.addEventListener('load', loadHandler, false);
</script>

</head>
<body>

<button id="btn-click-alert">Click to alert</button>
<button id="btn-noclick">No alert here</button>
<div id="data"> </div>
</body>
</html>

Can someone help?

Thanks!

Share Improve this question edited Sep 9, 2013 at 22:01 Thaddeus Albers 4,1925 gold badges36 silver badges43 bronze badges asked Sep 20, 2011 at 21:34 sunilsunil 313 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

I believe the visual, full-page alert being triggered on touch end is interfering with the touch event cycle. Try to call the alert after yielding to the DOM. eg.

setTimeout(function() {
    alert('btn clicked');
}, 0);
发布评论

评论列表(0)

  1. 暂无评论