Gar*_*ows 110 javascript iphone jquery click onclick
此功能在IE,Firefox和Chrome上完美运行,但在iPhone上,只有在点击时才能使用<img>
.点击页面(在img上的任何地方)都不会触发事件.
$(document).ready(function () {
$(document).click(function (e) {
fire(e);
});
});
function fire(e) { alert('hi'); }
Run Code Online (Sandbox Code Playgroud)
HTML部分非常基础,不应该是一个问题.
有任何想法吗?
Sim*_*ver 253
简短回答:
<style>
.clickable-div
{
cursor: pointer;
}
</style>
Run Code Online (Sandbox Code Playgroud)
更长的回答:
重要的是要意识到,如果你只是使用<a>
标签,一切都将按预期工作.您可以<a>
在iPhone 上的常规链接上错误地单击或拖动,一切都像用户期望的那样.
我想你有任意不可点击的HTML - 例如包含无法包装的文本和图像的面板<a>
.当我有一个我希望完全可点击的面板时,我发现了这个问题.
<div class='clickable-div' data-href="http://www.stackoverflow.com">
... clickable content here (images/text) ...
</div>
Run Code Online (Sandbox Code Playgroud)
要检测此div中任何位置的单击,我将使用带有data-href
html属性的jQuery,如上所示(此属性由我自己发明,不是标准的jQuery或HTML数据属性.)
$(document).on('click', '.clickable-div', function() {
document.location = $(this).data('href');
});
Run Code Online (Sandbox Code Playgroud)
无论您点击多少,这都适用于您的桌面浏览器,但不适用于iPad.
您可能想要将事件处理程序从- 更改click
为click touchstart
- 这确实会触发事件处理程序.但是,如果用户想要向上拖动页面(滚动),他们也会触发它 - 这是一种糟糕的用户体验.[你可能已经注意到偷偷摸摸的横幅广告的这种行为]
答案非常简单:只需设置CSS即可cursor: pointer
.
<style>
.clickable-div
{
cursor: pointer;
}
</style>
Run Code Online (Sandbox Code Playgroud)
这为桌面用户提供了额外的好处,可以通过手形图标指示该区域是可点击的.
Fáb*_*ima 33
改变这个:
$(document).click( function () {
Run Code Online (Sandbox Code Playgroud)
对此
$(document).on('click touchstart', function () {
Run Code Online (Sandbox Code Playgroud)
也许这个解决方案不适合您的工作,并且如回复中所述,这不是最佳解决方案.请检查其他用户的其他修复程序.
Gar*_*ows 30
添加以下代码有效.
问题是iPhone不会引发点击事件.他们提出"触摸"事件.非常感谢苹果.为什么他们不能像其他人一样保持标准?无论如何,感谢Nico的提示.
感谢: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);
}
Run Code Online (Sandbox Code Playgroud)
nru*_*tas 17
试试这个,只适用于iPhone和iPod,所以你不会让所有东西在chrome或firefox手机上变成蓝色;
/iP/i.test(navigator.userAgent) && $('*').css('cursor', 'pointer');
Run Code Online (Sandbox Code Playgroud)
基本上,在iOS上,默认情况下它们不是"可点击的" - 它们是"可触摸的"(pfffff)所以你通过给它们一个指针光标使它们"可点击".总的来说,对吧?
归档时间: |
|
查看次数: |
147011 次 |
最近记录: |