jQuery events on iOS 5

↘锁芯ラ 提交于 2019-12-06 01:55:30

问题


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).

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?


回答1:


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'); });


来源:https://stackoverflow.com/questions/7689004/jquery-events-on-ios-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!