2013-04-22 46 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/