2011-08-11 65 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