该功能在IE、Firefox和Chrome上都能完美运行,但在iPhone上,它只在点击<img>时才起作用.点击页面(除img外的任何地方)不会触发事件.

$(document).ready(function () {
  $(document).click(function (e) {
    fire(e);
  });
});

function fire(e) { alert('hi'); }

HTML部分非常基础,应该不会有问题.

Any ideas?

推荐答案

Adding in the following code works.

The problem is iPhones dont raise click events. They raise "touch" events. Thanks very much apple. Why couldn't they just keep it standard like everyone else? Anyway thanks Nico for the tip.

贷记:http://ross.posterous.com/2008/08/19/iphone-touch-events-in-javascript

$(document).ready(function () {
  init();
  $(document).click(function (e) {
    fire(e);
  });
});

function fire(e) { alert('hi'); }

function touchHandler(event)
{
    var touches = event.changedTouches,
        first = touches[0],
        type = "";

    switch(event.type)
    {
       case "touchstart": type = "mousedown"; break;
       case "touchmove":  type = "mousemove"; break;        
       case "touchend":   type = "mouseup"; break;
       default: return;
    }

    //initMouseEvent(type, canBubble, cancelable, view, clickCount, 
    //           screenX, screenY, clientX, clientY, ctrlKey, 
    //           altKey, shiftKey, metaKey, button, relatedTarget);

    var simulatedEvent = document.createEvent("MouseEvent");
    simulatedEvent.initMouseEvent(type, true, true, window, 1, 
                          first.screenX, first.screenY, 
                          first.clientX, first.clientY, false, 
                          false, false, false, 0/*left*/, null);

    first.target.dispatchEvent(simulatedEvent);
    event.preventDefault();
}

function init() 
{
    document.addEventListener("touchstart", touchHandler, true);
    document.addEventListener("touchmove", touchHandler, true);
    document.addEventListener("touchend", touchHandler, true);
    document.addEventListener("touchcancel", touchHandler, true);    
}

Jquery相关问答推荐

在 Mastodon 中将 jQuery 添加到 Rails 6

即使我使用 [FromBody] C# 从视图(Ajax)发送请求时在控制器中收到 NULL 对象

哪个 JQuery Select 器会排除与给定 Select 器匹配的父项的项目?

jQuery:使用变量作为 Select 器

如何用另一个响应替换窗口的 URL 哈希?

$(document).scrollTop() 总是返回 0

使用 jQuery 和 HTML 导出为 CSV

Facebook 风格的 JQuery 自动完成插件

jQuery Select 一个具有某个类的div,它没有另一个类

jquery click 不适用于 ajax 生成的内容

jQuery 对象和 DOM 元素

使用 D3.js(IE、safari 和 chrome)创建 SVG 后,如何保存/导出 SVG 文件?

如何更改 dropzone.js 中的默认文本?

使用 jQuery 从 AJAX 响应(json)构建表行

在 jQuery 中 Select 后代元素的最快方法是什么?

使用 jQuery 获取鼠标单击图像的 X/Y 坐标

如何将键和值都推送到 Jquery 中的数组中

jQuery append() 与 appendChild()

小于 10 给数字加 0

jQuery UI 选项卡 - 如何获取当前选定的选项卡索引