$(文档).click()在iPhone上无法正常工作。 jquery

这个function在IE,Firefox和Chrome上完美运行,但是当在iPhone上时,只有在点击<img>时才能工作。 点击页面(任何地方,但在img)不会触发事件。

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

HTML部分是非常基本的,不应该是一个问题。

有任何想法吗?

简短的回答:

 <style> .clickable-div { cursor: pointer; } </style> 

较长的答案:

意识到这一点很重要,如果你只是使用<a>标签,一切都会按预期工作。 您可以在iPhone上的常规<a>链接上点击或拖动,并且所有行为均按用户期望的方式进行。

我想象你有任意的HTML是不可点击的 – 比如包含文本和图片的面板不能用<a>包装。 当我有这样一个面板,我想完全可以点击的时候,我发现了这个问题。

 <div class='clickable-div' data-href="http://www.stackoverflow.com"> ... clickable content here (images/text) ... </div> 

要检测这个div中的任何地方的点击,我正在使用上面显示的data-href html属性的jQuery(这个属性是我自己发明的,而不是标准的jQuery或HTML数据属性)。

 $(document).on('click', '.clickable-div', function() { document.location = $(this).data('href'); }); 

无论您点击多less,这都可以在桌面浏览器上运行,但不能在iPad上运行。

你可能会试图改变你的事件处理程序click touchstart – 这确实会触发事件处理程序。 但是,如果用户想要拖动页面(滚动),他们也会触发它 – 这是一个糟糕的用户体验。

答案非常简单: 只需设置css cursor: pointer

 <style> .clickable-div { cursor: pointer; } </style> 

这对于桌面用户来说有额外的好处,用手形图标来指示该区域是可点击的。

感谢https://stackoverflow.com/a/4910962/16940

在下面的代码中添加工作。

问题是iPhone不会引发点击事件。 他们举起“触摸”事件。 非常感谢苹果。 为什么他们不能像其他人那样保持标准呢? 无论如何感谢尼科的提示。

信贷: 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); } 

改变这个:

 $(document).click( function () { 

对此

 $(document).on('click touchstart', function () { 

也许这个解决scheme不适合你的工作,就像在回复中描述的那样,这不是最好的解决scheme。 请检查另一个用户的另一个修复程序。

试试这个,只适用于iPhone和iPod,所以你不会让所有的东西都变成蓝色或Firefox的手机;

 /iP/i.test(navigator.userAgent) && $('*').css('cursor', 'pointer'); 

基本上,在iOS上,事情默认情况下不是“可点击的” – 它们是“可触摸的”(pfffff),所以通过给它们一个指针光标使它们“可点击”。 总是有道理的,对吗?

CSS Cursor:Pointer; 是一个很好的解决scheme FastClick https://github.com/ftlabs/fastclick是另一种解决scheme,如果您不想要;Cursor:Pointer;则不需要更改css Cursor:Pointer; 出于某种原因在元素上。 无论如何,我现在使用fastclick来消除iOS设备上的300ms延迟。

使用jQTouch代替 – 它的jQuery的移动版本

在移动iOS上,点击事件不会冒泡到文档主体,因此不能与.live()事件一起使用。 如果您必须使用非本地可点击元素(如div或section),请使用cursor:pointer; 在你的问题的元素非hover的CSS。 如果这是丑陋的,你可以看看委托()。

把这个包括到你的项目中。 检查github上的“自述文件”。 https://github.com/tomasz-swirski/iOS9-Safari-Click-Fix