onTouchEnd不适用于Android Froyo 2.2?

时间:2010-12-17 22:02:49

标签: javascript android

在Android 2.2上的默认浏览器中打开以下html会正确注册touchstart和touchmove事件,但不会触发事件。知道为什么吗?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
</head>
<body>
<div id="a" onTouchStart="touchstartFunction(event);"
            onTouchMove="touchmoveFunction(event);"
            onTouchEnd="touchendFunction(event);"
            style="width:300px ; height:300px;background:red;"></div>
<script>
    function touchstartFunction(event){
        event.preventDefault();
        var touch = event.touches[0];
        document.getElementById('touchMoveTextbox').value = "Touch start at " + touch.pageX + "x" + touch.pageY;
    }
    function touchmoveFunction(event){
        event.preventDefault();
        var touch = event.touches[0];
        document.getElementById('touchMoveTextbox').value = "Touch move at " + touch.pageX + "x" + touch.pageY;
    }
    function touchendFunction(event){
        event.preventDefault();
        var touch = event.touches[0];
        document.getElementById('touchMoveTextbox').value = "Touch end at " + touch.pageX + "x" + touch.pageY;
    }
</script>
<input type='textbox' size="30" id='touchMoveTextbox' >
</body>
</html>

1 个答案:

答案 0 :(得分:3)

相信你可能会遇到这样的情况: touchend event doesn't work on Android

因为你访问了touches [0],它在最后一种情况下为空并导致javascript错误。