JavaScript onTouch不起作用 [英] JavaScript onTouch not working
问题描述
有人可以告诉我为什么这个onTouch处理程序没有触发。
Can anybody tell me why this onTouch handler isn't firing.
var myDiv = document.getElementById('existingContent');
var myButton = '<a href="#" onClick="logOut();" ontouch="logOut()">log out</a>';
myDiv.appendChild(myButton);
function logOut() {
alert('hello');
}
我正在使用iPad 1.
I am using iPad 1.
推荐答案
我的建议与提议的建议相同这里:
My recommendation would be the same as the one proposed here on MDN:
var el = document.getElementsByTagName("canvas");
el.addEventListener("touchstart", handleStart, false);
我对ontouch属性的犹豫是跨设备支持。为此,您可以考虑功能检测。
My hesitation with the ontouch attribute is cross-device support. For this you might consider feature detection.
Modernizr 是一个确定是否在运行时启用触摸的常用方法。
Modernizr is a popular way to determine if touch is enabled at runtime.
如果启用了上述事件,则触摸或不触摸的类将添加到html元素并用于确定上面的代码是否应该运行。
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.
编辑:
在后来的几天里,我遇到了此文档描述了触摸事件。与大部分技术一样,还有更多......
In subsequent days I ran across this document describing Touch Events. As with much of technology, there is still more to it...
这篇关于JavaScript onTouch不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!