我正在修改 js 中的 touchevents
。我在 eclipse 的 logcat 中遇到过这个错误。
document.getElementById("squareBracket").
addEventListener("touchmove", touchHandler, false);
document.getElementById("squareBracket").
addEventListener("touchend", touchHandler, false);
function touchHandler(e) {
if (e.type == "touchstart") {
alert("You touched the screen!");
} else if (e.type == "touchmove") {
// alert(e.changedTouches[0].pageX);
// alert(e.changedTouches[0].pageY);
} else if (e.type == "touchend" || e.type == "touchcancel") {
alert('X :' + e.targetTouches[0].pageX);
alert('Y :' + e.targetTouches[0].pageY);
}
}
如果我在 touchmove
中删除 if
中的注释,坐标弹出。但是,如果它被评论,我的 logcat 中的错误就会出现。
最佳答案
您应该从这里开始了解 targetTouches、changedTouches 和 touches 的区别:Variation of e.touches, e.targetTouches and e.changedTouches
在你的情况下,在 touchend 或 touchcancel 的那一刻,targetTouches 列表是空的,信息保留在 changedTouches 中。
将您的代码更改为:
alert('X :' + e.changedTouches[0].pageX);
alert('Y :' + e.changedTouches[0].pageY);
应该可以解决问题。
关于javascript - 未捕获的类型错误 : Cannot read property 'pageX' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23625782/