我用jquery mobile'tap'事件测试过,发现它被触发了两次,每次触发。代码如下的html页面:jQuery手机触发事件触发两次
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.js"></script>
<style>
#box{
background-color:red;
width:200px;
height:200px;
}
</style>
</head>
<body>
<div id="box">
tapped me
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#box').bind('tap',function(event) {
alert('why');
});
});
</script>
</body>
</html>
更讽刺的是,当我测试过这个的jsfiddle,它只触发事件为一次。
这是链接。 http://jsfiddle.net/mochatony/tzQ6D/6/
经过进一步测试后,我发现这个bug已经消失,将javascript放在标题部分。
<!DOCTYPE html>
<html>
<head>
<title>Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<script type="text/javascript" src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.js">
</script>
<style type="text/css">
#box{ background-color:red; width:200px; height:200px; }
</style>
<script type="text/javascript">
$(document).ready(function() {
$('#box').bind('tap', function(event) {
alert('halo');
});
});
</script>
</head>
<body>
<div id="box">
tapped me
</div>
</body>
</html>
我无法解释为什么在body和header部分的放置使这个不同。
感谢您的答复。用pageinit做了一次测试,pageinit事件也被触发了两次。我测试了Firefox和Chrome。对我来说似乎很奇怪。 – TonyTakeshi