iOS 5上的jQuery事件 [英] jQuery events on iOS 5
问题描述
我在jQuery 1.6.4,iOS 5和touchstart/touchend事件的注册方面遇到了问题(很明显,如标题中所述).
I'm having an issue with jQuery 1.6.4, iOS 5 and the registration of touchstart/touchend events (as stated in the title, obviously).
采用以下代码:
<!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>
纯JS"按钮在iOS 4.3和iOS 5中显示警报,但"jQuery"按钮仅在iOS 4.3上有效.
在iPad/iPhone模拟器4.3和5上进行了测试;还在真实的iPhone 4.3,iPhone 5.0和iPad 5.0上进行了测试.
如果我使用<input type="button">
甚至是简单的<a>
而不是<button>
,也会产生相同的反应.
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>
.
我相信这是与jQuery相关的问题吗?
Is it a problem related to jQuery as I believe?
推荐答案
在Apple开发人员论坛上一个人的回答:我只需要在 之后将bind()绑定到DOM,像这样:
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'); });
这篇关于iOS 5上的jQuery事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!