7

谁能告诉我为什么 touchenter 事件在此代码中不起作用。mouseenter 在桌面上工作正常。应该很简单,但我错过了一些东西。

这里的例子 - http://jsfiddle.net/gCEqH/6/

完整代码如下:

<!DOCTYPE html>
<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    </head>

    <body>
        <img id="myImg" src="http://jackiehutchings.com/wp-content/uploads/2011/09/g-plus-icon-96x96.png" />        

        <script>
            $(window).load(function() { 
            $('#myImg').on("touchenter mouseenter", function(event){
                alert('entered!');
            });
        });
        </script>
    </body>
</html>
4

1 回答 1

0

也许这样的事情会起作用?

var elementIdTouching = "";
$('body').on("touchmove", function(e){
    var tList = e.touches; // get list of all touches
    for (var i = 0; i < tList.length; i++) {
        var thisTouch = tList[i]; // not 100% sure about this
        var elementTouching = document.elementFromPoint( 
            thisTouch.screenX, 
            thisTouch.screenY
        );
        if (elementTouching.id != elementIdTouching) {
            elementIdTouching = elementTouching.id;
            if (elementTouching.id == "myImg") {
                alert("entered!");
            }
        }
    }
}).on("touchend", function(e){
    elementIdTouching = "";
});
$('#myImg').on("mouseenter", function(e){
    alert('entered!');
});

tList ~ https://developer.mozilla.org/en-US/docs/Web/API/TouchList

免责声明:我没有测试过这个。

于 2013-12-13T23:08:29.083 回答