javascript 使 onclick 在 iphone 上工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18914568/
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
Make onclick work on iphone
提问by user1087110
I have been using "onclick" in my javascript, but now I want it to work on Iphone as well. Is there a simple way to make all "onclick" to work like ontouchstart for devices that support ontouchstart?
我一直在我的 javascript 中使用“onclick”,但现在我希望它也能在 Iphone 上运行。对于支持 ontouchstart 的设备,是否有一种简单的方法可以使所有“onclick”像 ontouchstart 一样工作?
Or do I need to write all script twice (one that uses onclick and one that uses ontouchstart)? :S
或者我是否需要编写所有脚本两次(一个使用 onclick,一个使用 ontouchstart)?:S
Note: I dont want to use jquery or any other library.
注意:我不想使用 jquery 或任何其他库。
<!DOCTYPE html>
<title>li:hover dropdown menu on mobile devices</title>
<script>
window.onload = function () {
if ('ontouchstart' in window) {
// all "onclick" should work like "ontouchstart" instead
}
document.getElementById('hbs').onclick = function () {
alert('element was clicked');
}
}
</script>
<a id=id1 href=#>button</a>
回答by Martijn
This post got more attention, so I'm going to add another commonly used, more up to date, trick:
这篇文章受到了更多关注,所以我将添加另一个常用的、最新的技巧:
// First we check if you support touch, otherwise it's click:
let touchEvent = 'ontouchstart' in window ? 'touchstart' : 'click';
// Then we bind via thát event. This way we only bind one event, instead of the two as below
document.getElementById('hbs').addEventListener(touchEvent, someFunction);
// or if you use jQuery:
$('#hbs').on(touchEvent, someFunction);
The let touchEvent
should be declared out of function (also not in a document.ready) at the top of your javascript. This way you can use this in all your javascript. This also allows easy jQuery usage.
本let touchEvent
应宣告出局的功能(也没有在的document.ready)在您的JavaScript的顶部。这样你就可以在你所有的 javascript 中使用它。这也允许简单的 jQuery 使用。
Old answer:
旧答案:
This fixes the need for copying your code (well at least to a minimum). Not sure if you can combine them
这解决了复制代码的需要(至少是最低限度)。不知道你是否可以将它们结合起来
function someFunction() {
alert('element was clicked');
}
document.getElementById('hbs').onclick = someFunction;
document.getElementById('hbs').ontouchstart= someFunction;
document.getElementById('hbs')
.addEventListener('click', someFunction)
.addEventListener('touchstart', someFunction);
回答by aburabee3
The reason javascript is not working on the iPhone in Safari is because iPhones have the option to turn off Javascript.
javascript 在 Safari 中无法在 iPhone 上运行的原因是因为 iPhone 可以选择关闭 Javascript。
Go to "Settings" Then scroll down to "Safari" Then scroll all the way to the bottom "Advanced" Then turn on Javascript!
转到“设置”然后向下滚动到“Safari”然后一直滚动到底部“高级”然后打开Javascript!
Also, you can check if Javascript is enabled with this code:
此外,您可以使用以下代码检查是否启用了 Javascript:
<script type="text/javascript">
document.write("Javascript is working.")
</script>
<noscript>JavaScript is DISABLED!!!!!!</noscript>