jQuery 单击事件在 iOS 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14795944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:55:55  来源:igfitidea点击:

jQuery click events not working in iOS

jqueryioscssclicktransform

提问by Bobe

Second update:Looks like one of my functions (resetFigures) was preventing the event handler, so moving that to the end of the bind function sorted it out.

Update:I realized after some basic testing that the click events were registering, it's just that the box fails to flip when tapped.

第二次更新:看起来我的一个函数 (resetFigures) 阻止了事件处理程序,因此将其移到绑定函数的末尾将其整理出来。

更新:经过一些基本测试后,我意识到点击事件正在注册,只是点击时该框无法翻转。

I have the basic aesthetic functionality of my site working in Chrome and Firefox, but it refuses to behave properly on iOS (test on iPhone 4 with iOS 6.1 and iPad with iOS 4.3.5).

我的网站在 Chrome 和 Firefox 中具有基本的美学功能,但它拒绝在 iOS 上正常运行(在使用 iOS 6.1 的 iPhone 4 和使用 iOS 4.3.5 的 iPad 上进行测试)。

You can view the site and of course the scripts (main.js) here: http://bos.rggwebdesigns.com/

您可以在此处查看站点,当然还有脚本 (main.js):http://bos.rggwebdesigns.com/

I've read that iOS doesn't really handle jQuery click events properly, but I'm struggling to figure out a fix. A couple threads here on Stack Overflow mentioned the live() method, but implementing it like follows (as well as adding onclick=""to the clickable elements) didn't seem to work:

我读过 iOS 并没有真正正确处理 jQuery 单击事件,但我正在努力找出修复方法。Stack Overflow 上的几个线程提到了 live() 方法,但如下实现它(以及添加onclick=""到可点击元素)似乎不起作用:

$('.card').live('click touchstart', function() {
        var figure = $(this).children('.back');
        var button = figure.find('.button');
        var column = $(this).parents().eq(1);
        $('.column').removeAttr('style');
        column.css('z-index', 2000);
        resetFigures();
        if(flipCard(this)){
            swoosh.pause();
            swoosh.currentTime = 0;
            swoosh.play();
        }
    });

I also came across this interesting workaround project: http://aanandprasad.com/articles/jquery-tappable/. However, I had no luck with that either:

我还遇到了这个有趣的变通项目:http: //aanandprasad.com/articles/jquery-tappable/。但是,我也没有运气:

$('.card').tappable(function() {
        var figure = $(this).children('.back');
        var button = figure.find('.button');
        var column = $(this).parents().eq(1);
        $('.column').removeAttr('style');
        column.css('z-index', 2000);
        resetFigures();
        if(flipCard(this)){
            swoosh.pause();
            swoosh.currentTime = 0;
            swoosh.play();
        }
    });

Also, please correct me if I've been mislead, but according to this site, 3D transforms are supported in iOS with the appropriate prefixes: http://caniuse.com/transforms3d

另外,如果我被误导了,请纠正我,但根据此站点,iOS 支持 3D 转换并带有适当的前缀:http: //caniuse.com/transforms3d

回答by BingeBoy

There is an issue with iOS not registering click/touch events bound to elements added after DOM loads.

iOS 存在一个问题,即未注册绑定到 DOM 加载后添加的元素的点击/触摸事件。

While PPK has this advice: http://www.quirksmode.org/blog/archives/2010/09/click_event_del.html

虽然 PPK 有这个建议:http: //www.quirksmode.org/blog/archives/2010/09/click_event_del.html

I've found this the easy fix, simply add this to the css:

我发现这是一个简单的解决方法,只需将其添加到 css 中:

cursor: pointer;

回答by saurabh kamble

Recently when working on a web app for a client, I noticed that any click events added to a non-anchor element didn't work on the iPad or iPhone. All desktop and other mobile devices worked fine - but as the Apple products are the most popular mobile devices, it was important to get it fixed.

最近在为客户端开发 Web 应用程序时,我注意到添加到非锚元素的任何点击事件在 iPad 或 iPhone 上都不起作用。所有台式机和其他移动设备都运行良好 - 但由于 Apple 产品是最受欢迎的移动设备,因此修复它很重要。

Turns out that any non-anchor element assigned a click handler in jQuery must either have an onClick attribute (can be empty like below):

事实证明,任何在 jQuery 中分配了单击处理程序的非锚元素都必须具有 onClick 属性(可以为空,如下所示):

onClick=""

OR

或者

The element css needs to have the following declaration:

元素 css 需要具有以下声明:

cursor:pointer

Strange, but that's what it took to get things working again!
source:http://www.mitch-solutions.com/blog/17-ipad-jquery-live-click-events-not-working

奇怪,但这就是让事情重新工作的原因!
来源:http: //www.mitch-solutions.com/blog/17-ipad-jquery-live-click-events-not-working

回答by elio.d

You should bind the tap event, the click does not exist on mobile safari or in the UIWbview. You can also use this polyfill,to avoid the 300ms delay when a link is touched.

您应该绑定点击事件,移动 safari 或 UIWbview 中不存在点击。您还可以使用此polyfill来避免触摸链接时的 300 毫秒延迟。