Javascript 在 iPad 上捕捉 Click on DOM/HTML/BODY 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6397603/
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
Catching the Click on DOM/HTML/BODY event on the iPad
提问by Jim
I'm using jQuery to detect a click on the DOM - or let's every click.
我正在使用 jQuery 来检测对 DOM 的点击 - 或者让我们每次点击。
$(document).click(function(){
alert("Click :-)");
});
This works pretty good in every browser except Safari for iPad/iPhone. I've also tried to apply the event on the html or body element - no way. How to detect a common click on the iPad/iPhone?
这在除 Safari for iPad/iPhone 之外的所有浏览器中都非常有效。我还尝试在 html 或 body 元素上应用该事件 - 没办法。如何检测 iPad/iPhone 上的常见点击?
Best regards, Jim
最好的问候,吉姆
回答by guigouz
As I found on http://www.danwellman.co.uk/fixing-jquery-click-events-for-the-ipad/you may test the user agent and use touchstart or click depending on the platform
正如我在http://www.danwellman.co.uk/fixing-jquery-click-events-for-the-ipad/ 上发现的,您可以测试用户代理并根据平台使用 touchstart 或 click
var ua = navigator.userAgent,
event = (ua.match(/iPad/i)) ? "touchstart" : "click";
$(document).on(event, function (ev) {
...
});
回答by mlhDev
These answers got me started on the right direction (first google for "jquery document.on ipad"), so thanks for that, but borrowing from this answerI simplified it to something like this:
这些答案让我开始了正确的方向(第一个谷歌搜索“jquery document.on ipad”),所以谢谢你,但从这个答案中借用我把它简化为这样的:
$(document).on("click touchstart", function (ev) {
...
});
This would obviously produce undesired results if a platform (like Android or Windows Phone or something) supported both click and touchstart in the event bubble, so if anyone knows that let me know (and I'll fix my code and delete this answer! :)
如果平台(如 Android 或 Windows Phone 或其他东西)在事件气泡中同时支持 click 和 touchstart,这显然会产生不希望的结果,所以如果有人知道请告诉我(我将修复我的代码并删除这个答案!: )
回答by namretiolnave
I have used this:
我用过这个:
jQuery(document).on('touchstart',function(event){
//your code here
});
-also instead of "document" you can bind to any DOM object.
- 也可以代替“文档”绑定到任何 DOM 对象。
and this(from another forum answer):
这(来自另一个论坛的答案):
var body = document.getElementsByTagName('body')[0];
if ("ontouchstart" in window) {
body.ontouchstart = function(){
//your code here
};
};
-again, you don't have to use 'body' you can assign a variable to an object by class this way:
- 同样,您不必使用“主体”,您可以通过这种方式按类将变量分配给对象:
var dd = document.getElementsByClassName('infoAction')[0];
回答by Thariama
You may use the touchstart
event instead.
您可以改用该touchstart
事件。
回答by Billy Moon
$('html').click(function(){
alert("Click :-)");
});
This works for me, I tested it now.
这对我有用,我现在测试了它。
Even works with no content on page, wherever you click on the page.
即使在页面上没有内容的情况下也能工作,无论您在何处单击页面。
回答by gabuk
You can attach the click listener to the main wrapper element (say div that encloses all the components in your page).
您可以将单击侦听器附加到主要包装元素(例如包含页面中所有组件的 div)。
回答by figolu
<body><div onclick="void(0)">
... your page tags ...
</div></body>
There is a minor difference with others browsers behaviour: the document object will reveive click events only for tags located inside the "... your page tags ..." section.
与其他浏览器的行为略有不同:文档对象将仅针对位于“...您的页面标记...”部分内的标记重新显示点击事件。
In other words, suppose your html and body tags have a yellow background color, and their child tags have a red background color. The document object will receive clicks on the red areas only. This is usually not a serious drawback.
换句话说,假设您的 html 和 body 标签具有黄色背景色,而它们的子标签具有红色背景色。文档对象将仅接收对红色区域的点击。这通常不是一个严重的缺点。
Tested on an iPhone 3 only
仅在 iPhone 3 上测试