【发布时间】:2012-02-22 00:18:45
【问题描述】:
我有一个移动 Web 画廊页面,其中有一个 CSS 浮动“下一个”链接。 CSS float 属性使链接具有 display: block 行为。 jQuery touchstart 事件绑定到链接。当用户单击该链接时,绑定到该 touchstart 事件的 Javascript 代码通过 Ajax 将画廊推进一张幻灯片。也就是说,没有页面刷新。
但是,我注意到偶尔当我触摸链接块空间中不是链接文本本身的区域时,浏览器会跟随 href 并导致页面刷新(因为 URL 已更改)而不是执行 Javascript 代码绑定到 touchstart 事件。
以前有人见过吗?有没有办法解决这个问题?
我将其简化为下面的代码,它仍然会发生,尽管频率要低得多。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<title>Test</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
<style type='text/css'>
.cont { width: 320px; }
.next { border-left: 1px solid #000; float: right; text-align: right; width: 65px; }
.msg { clear: both; width: 200px; }
</style>
<script type='text/javascript'>//<![CDATA[
$(function(){
$('.next').bind('click touchstart', function(event) {
event.preventDefault();
if (event.type == 'touchstart') {
$(this).unbind('click');
}
$('.msg').append('<p>Click!</p>');
});
});//]]>
</script>
</head>
<body>
<div class="cont"><a href="http://www.yahoo.com" class="next">Next</a></div>
<div class="msg"></div>
</body>
</html>
【问题讨论】:
标签: javascript jquery android iphone browser