2011-10-07 19 views
6

Ho riscontrato un problema con jQuery 1.6.4, iOS 5 e la registrazione di eventi touchstart/touchend (come indicato nel titolo, ovviamente).eventi jQuery su iOS 5

Prendere il seguente codice:

<!DOCTYPE html> 
<html lang="fr"> 
<head> 
    <meta charset="utf-8" /> 
    <script type="text/javascript" src="mmpa/jquery-1.6.4.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      var $body = $('body'); 
      $('<button>').html('test jQuery').bind('touchstart', function() { alert('touchstart'); }).appendTo($body); 
     }); 
    </script> 
</head> 
<body> 
    <button ontouchstart="alert('touchstart');">test pure JS</button> 
</body> 
</html> 

Il pulsante "puro JS" mostra l'avviso di iOS 4.3 e iOS 5, ma il pulsante "jQuery" funziona solo su iOS 4.3. Testato su simulatore iPad/iPhone, 4.3 e 5; testato anche su iPhone 4, iPhone 5.0 e iPad 5.0 reali. Stessa reazione se uso uno <input type="button"> o anche un semplice <a> anziché uno <button>.

È un problema correlato a jQuery come credo?

+0

non usare bind ('TouchStart') e ontouchstart = "". Usane uno e preferisci non usare javascript in linea. –

+1

Questa era una dimostrazione del concetto per mostrare che JS in linea funziona quando jQuery no. Ovviamente non uso mai, mai usato JS inline in codice reale. – Cyrille

+0

Sono stato in grado di produrre questo errore con jQ 1.5.2 e l'evento click, quindi forse dovresti modificare il titolo in qualcosa come "jQuery Events su iOS 5" – webXL

risposta

8

Risposto da un ragazzo su dev forum di Apple: Ho bisogno di legare() solo dopo l'elemento è stato aggiunto al DOM, in questo modo:

var btn = $('<a>').html('test jQuery').appendTo($body); 
btn.bind('touchstart', function(e) { alert('touchstart'); }); 
+2

Cosa succede se usi live() invece di bind() ? Qualche spiegazione del motivo per cui ha smesso di funzionare in iOS 5? – Crashalot