2013-04-22 48 views
1

我注意到,。对( “点击”)不起作用在iOS

$("body").on("click", "#id", function(event) {... 

不同时

$("#id").on("click", function(event) {... 

作品完美地在iOS上运行。相同的站点,相同的jQuery(最新),相同的DOM。我无法使用后者,因为#id是动态添加的。

想法?

+0

[这里没有问题](http://jsfiddle.net/mblase75/YBxyw/)。也许问题在你的代码中的其他地方。如果您将iOS设备连接到Mac,则可以使用桌面Safari在移动Safari中查看控制台错误:http://developer.apple.com/library/ios/#documentation/AppleApplications/Reference/SafariWebContent/DebuggingSafarioniPhoneContent/DebuggingSafarioniPhoneContent。 html – Blazemonger 2013-04-22 19:13:43

+0

我这样做,并总是在我的Mac上使用safari进行调试。 @PalashMondal解决了我的问题。 – 2013-04-22 19:21:16

回答

13

尝试如下一次:

$(document).on("click touchstart", "#id", function(event) {... 
+0

仍然没有运气,工作在OSX/Safari浏览器,而不是iOS版... – 2013-04-22 19:06:33

+0

只是好奇 - 究竟是使用'document',而不是'body'效果? – 2013-04-22 19:08:21

+0

@RaphaelJeger:更新了代码。看看它现在在iOS上的工作.. – 2013-04-22 19:09:24

1

如果你想添加点击不使用触摸事件,你可以做到这一点(虽然它不涉及代理嗅探):

// Check if it is iOS 
var isiOS = (navigator.userAgent.match(/(iPad|iPhone|iPod)/g) ? true : false); 

if(isiOS === true) { 

    // Store -webkit-tap-highlight-color as this gets set to rgba(0, 0, 0, 0) in the next part of the code 
    var tempCSS = $('a').css('-webkit-tap-highlight-color'); 

    $('body').css('cursor', 'pointer')         // Make iOS honour the click event on body 
      .css('-webkit-tap-highlight-color', 'rgba(0, 0, 0, 0)');  // Stops content flashing when body is clicked 

    // Re-apply cached CSS 
    $('a').css('-webkit-tap-highlight-color', tempCSS); 

} 

多见于http://www.texelate.co.uk/blog/post/124-add-a-click-event-to-html-or-body-on-ios-using-jquery/