jQuery events on iOS 5
I'm having an issue with jQuery 1.6.4, iOS 5 and the registration of touchstart/touchend events (as开发者_C百科 stated in the title, obviously).
Take the following code:
<!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>
The "pure JS" button shows the alert in iOS 4.3 and iOS 5, but the "jQuery" button only works on iOS 4.3.
Tested on iPad/iPhone simulator, 4.3 and 5 ; also tested on real iPhone 4.3, iPhone 5.0, and iPad 5.0.
Same reaction if I use a <input type="button">
or even a simple <a>
instead of a <button>
.
Is it a problem related to jQuery as I believe?
Answered by a guy on Apple dev forums: I need to bind() only after the element has been added to the DOM, like so:
var btn = $('<a>').html('test jQuery').appendTo($body);
btn.bind('touchstart', function(e) { alert('touchstart'); });
精彩评论