JavaScript onTouch 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10956637/
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
JavaScript onTouch not working
提问by ShadeTreeDeveloper
Can anybody tell me why this onTouch handler isn't firing.
谁能告诉我为什么这个 onTouch 处理程序没有触发。
var myDiv = document.getElementById('existingContent');
var myButton = '<a href="#" onClick="logOut();" ontouch="logOut()">log out</a>';
myDiv.appendChild(myButton);
function logOut() {
alert('hello');
}
I am using iPad 1.
我正在使用 iPad 1。
回答by Hyman Stone
My recommendation would be the same as the one proposed hereon MDN:
我的建议是一样提出了一个位置上的MDN:
var el = document.getElementsByTagName("canvas");
el.addEventListener("touchstart", handleStart, false);
My hesitation with the ontouch attribute is cross-device support. For this you might consider feature detection.
我对 ontouch 属性的犹豫是跨设备支持。为此,您可以考虑特征检测。
Modernizris a popular way to determine if touch is enabled at runtime.
Modernizr是一种确定是否在运行时启用触摸的流行方法。
If the events above are enabled, the classes touch or no-touch would be added to the html element and used to determine if the code above should run.
如果启用了上面的事件,类 touch 或 no-touch 将被添加到 html 元素并用于确定上面的代码是否应该运行。
EDIT:
编辑:
In subsequent days I ran across thisdocument describing Touch Events. As with much of technology, there is still more to it...
在随后的日子里,我跑过这个文档描述了触摸事件。与许多技术一样,它还有更多……
回答by Jencibácsi
There is no "touch"
event at all. You can find touchstart
, touchend
, touchmove
and touchcancel
events, as is shown in this link. I think you must use touchstart
in your HTML:
根本没有"touch"
事件。你可以找到touchstart
,touchend
,touchmove
和touchcancel
事件,在显示该链接。我认为你必须touchstart
在你的 HTML 中使用:
ontouchstart="logOut()"
Also see this webpage.
另请参阅此网页。
回答by ADVAIT KALE
Try using onfocus="logOut()"instead of ontouch="logOut()"
尝试使用onfocus="logOut()"而不是 ontouch="logOut()"