Jquery不能与phoneGap一起工作

Jquery not working with phoneGap

本文关键字:一起 工作 phoneGap 不能 Jquery      更新时间:2023-09-26

我正在使用phonegap,这段代码可以在我的android手机上完美加载。但是当我点击按钮时,它不显示文本。

    <!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" charset="utf-8" src="cordova-2.4.0.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#btn1").click(function(){
    $("#test1").text("Hello world!");
  });
});
</script>
</head>
<body>
<p id="test1"></p>
<button id="btn1">Set Text</button>
</body>
</html>

我不认为JQuery正在加载到页面中。

你引用它为

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">

表示使用当前页面来自的任何协议。在移动设备上,从file://获取脚本的实际请求是:

file://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js

您需要指定要使用的方案,否则将其包含在PG项目本身中。

在使用移动设备时,强烈建议您使用移动版的jquery, jquery.mobile.js

然后你可以使用vclick,一切都很好:

处理触摸端或鼠标单击触摸事件的规范化事件设备。

$(document).ready(function(){
     $("#btn1").vclick(function(){
       $("#test1").text("Hello world!");
     });
});

试试:

<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript" charset="utf-8" src="cordova-2.4.0.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
  </script>
  <script>
     $(document).on('pageinit', function(event){
        $("#btn1").click(function(){
            $("#test1").text("Hello world!");
        });
     });
  </script>
  <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js">
  </script>
</head>
<body>
   <p id="test1"></p>
   <button id="btn1">Set Text</button>
</body>
</html>

使用jQuerymobile的pageinit代替doc ready开发移动应用