为什么触摸事件不起作用?

时间:2014-03-29 09:56:56

标签: cordova javascript-events

我正试图为触摸事件做一个警告框,但我没有工作。我已经上传到Phonegap版本并将其安装在我的手机中,但它无法识别touchstart事件。

这是代码:

的index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>

    </head>
    <body>
        <div id="box"></div>

        <div id="box2"></div>

        <div id="box3"></div>



        <script type="text/javascript" src="cordova.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

        <script type="text/javascript" src="js/index.js"></script>
        <script>
               $(document).one('deviceready', App.initialize());
        </script>
    </body>
</html>

index.css

#box {
    width: 300px;
    height: 300px;
    background-color: yellow;
}
#box2 {
    width: 300px;
    height: 300px;
    background-color: red;  
}

#box3 {
    width: 300px;
    height: 300px;
    background-color: blue;
}

index.js

var App = {
    initialize: function() {
        alert("App initialized");
        this.clickEvents();
    },
    clickEvents: function() {
        $("#box").on('touchstart',function () {
            alert("Yellow box jQuery and touchstart event");
        });
        $("#box2").on('touchstart',function (){
             alert("Red box jQuery and touchstart event");
        });
        $("#box3").on('touchstart', function (){
             alert("Blue box jQuery and touchstart event");
        });
    }
  }

2 个答案:

答案 0 :(得分:0)

更改

$(document).one('deviceready', App.initialize()); 

$(document).on('deviceready', App.initialize()); 

答案 1 :(得分:0)

可能为时已晚...... 您应该将App.initialize作为回调参数处理:

$(document).on('deviceready', App.initialize);