0

我正在使用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>
4

3 回答 3

2

我不认为 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 项目本身中。

于 2013-02-28T11:02:37.530 回答
1

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

然后你可以使用vclick一切正常:

用于处理触摸设备上的 touchend 或鼠标单击事件的规范化事件。

$(document).ready(function(){
     $("#btn1").vclick(function(){
       $("#test1").text("Hello world!");
     });
});
于 2013-02-28T05:44:25.790 回答
0

尝试使用这个:

<!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>

如果为移动应用程序开发,而不是doc ready使用pageinitjQuerymobile。

于 2013-02-28T05:58:50.997 回答