2011-08-11 136 views
2

这是一个令我发疯的新手问题。根据jQuery mobile site,我已将jQueryjQuery mobile纳入HTML的headjQuery手机重复事件

然后,当我为任何事件分配监听时,它会运行两次。例如:

<!doctype html> 
<html> 
    <head> 
     <meta http-equiv="imagetoolbar" content="false" /> 
     <meta name="apple-mobile-web-app-capable" content="yes" /> 
     <meta name="apple-mobile-web-app-status-bar-style" content="black" /> 
     <style> 
      .slide {width:400px; height:400px; padding:40px; border:1px solid black;} 
     </style> 
     <link href="http://code.jquery.com/mobile/latest/jquery.mobile.min.css" rel="stylesheet" type="text/css" /> 
     <script src="http://code.jquery.com/jquery-1.6.2.min.js"></script> 
     <script src="http://code.jquery.com/mobile/latest/jquery.mobile.min.js"></script> 
    </head> 
    <body> 
     <div class="slide"> 
      <h1>1</h1> 
     </div> 
     <script type="text/javascript">  
      $(function() { 
         $('.slide').tap(function() { 
          alert('tapped!'); 
         }); 
        }); 
     </script> 
    </body> 
</html> 

然后,如果我点击(或点击在桌面浏览器),该滑动件的div 两个警报示。如果我对包含jQuery Mobile的行进行评论,它只会运行一次。

这里有什么线?

回答

5

如果您将脚本移动到<head>标记中,它只会触发一次。我认为这与疯狂的jQuery Mobile页面缓存有关。

+0

太棒了,它工作正常。感谢:] –

+0

如果你再次碰到这个问题(我做过),那么你可以通过'die'再次移除绑定事件。不过,我想这是不好的做法。 - http://api.jquery.com/die/ – Smamatti