在phonegap中使用jquery

时间:2016-06-03 23:20:12

标签: jquery cordova

嗨,我开始使用phonegap,我在点击按钮时遇到了一些问题。

我最好的机会是:

的index.html:

<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>Hello World</title>
    </head>
    <body>
        <button id="botonPrueba">pulsame</button>
        <div id="mostrar"></div>
        <script type="text/javascript" src="resources//jquery-1.12.3.min.js"></script>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>

    </body>
</html>

index.js:

function muestraAction() {
    $("#mostrar").html("ola");
}

$(document).ready(function () {
    // jQuery is properly loaded at this point
    // so proceed to bind the Cordova's deviceready event
    $(document).bind("deviceready", function () {

        $("#botonPrueba").bind('click', muestraAction());

    });
});

在普通网页上,所有工作都很好,但手机屏幕没有。

哪里错了?

浏览器中的

说我找不到cordova.js,但在模拟器中没有。

1 个答案:

答案 0 :(得分:1)

  1. 您在浏览器中遇到cordova.js问题的原因是因为它在您构建应用程序时被注入。没问题。
  2. 你的代码有点奇怪,我会建议这个;

    function muestraAction() {
        $("#mostrar").html("ola");
    }
    
    document.addEventListener("deviceready", onDeviceReady, false);
    
    $(document).ready(function () {
    
        function onDeviceReady() {
    
            $("#botonPrueba").on('click', function () {
              muestraAction();
            )};
    
            // or even better if using jQuery mobile
    
            $("#botonPrueba").on('vclick', function () {
              muestraAction();
            )};
    
        });
    });