【问题标题】:jQuery Mobile breaks Phonegap deviceready eventjQuery Mobile 打破了 Phonegap deviceready 事件
【发布时间】:2013-02-05 06:17:22
【问题描述】:

所以我已经让 jQuery 1.8.2 与 Phonegap 一起使用没有问题,但是只要我添加 jquery.mobile.1.2.0,默认的 Phonegap 示例就会中断。 deviceready 事件停止触发。

index.html

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="format-detection" content="telephone=no" />
    <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 class="app">
        <h1>Apache Cordova</h1>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p>
        </div>
    </div>
    <input   type="text" name="firstname" id="firstname" />  
    <a href="#" class="btn" onclick="displayHello();">Say Hello</a>
    <script type="text/javascript" src="cordova-2.4.0.js"></script>
    <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
    <script type="text/javascript" src="js/jquery.mobile-1.2.0.min.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
        function displayHello(){
            var name = document.getElementById("firstname").value;  
            navigator.notification.alert("My name is "+ name);  
        }
    </script>
</body>
</html>

index.js

var app = {
// Application Constructor
initialize: function() {
    this.bindEvents();
},
// Bind Event Listeners
//
// Bind any events that are required on startup. Common events are:
// 'load', 'deviceready', 'offline', and 'online'.
bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},
// deviceready Event Handler
//
// The scope of 'this' is the event. In order to call the 'receivedEvent'
// function, we must explicity call 'app.receivedEvent(...);'
onDeviceReady: function() {
    app.receivedEvent('deviceready');
},
// Update DOM on a Received Event
receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
}
};

所以这是Phonegap自带的默认代码示例,我只是添加了

<script type="text/javascript" src="js/jquery.mobile-1.2.0.min.js"></script>

在 index.html 中。

不确定发生了什么,因为似乎其他人已经让 Phonegap 和 jQuery Mobile 一起工作得很好。

我已经尝试过将 js 简化,只调用 deviceready 事件。

我已经尝试按照这个解决方案和下面发布的另一个解决方案无济于事。

Correct way of using JQuery-Mobile/Phonegap together?

但同样的事情也会发生。使用 jQuery Mobile deviceready 永远不会触发,没有它,它可以正常触发。

任何帮助将不胜感激!也许我只是在这里遗漏了一些简单的东西。

我还尝试了 jQuery 和 jQuery Mobile 的不同版本组合,但没有成功。 我正在运行 Android Phonegap 版本和 cordova-2.3.0。我最近尝试升级到 cordova-2.4.0 看看是否有帮助,但没有任何帮助......

更新:LogCat/DDMS 中没有抛出错误

【问题讨论】:

  • 我在正文中加载了所有脚本,我将它们移动到 并且它似乎工作。我目前正在另一台计算机上对其进行测试,我回家后也会尝试以确保。正如我之前应该做的那样,我查找了 phonegap 的 onDeviceReady 事件,他们的完整示例在头部加载了 javascript 并使用 调用 app.initialize(); docs.phonegap.com/en/2.4.0/…
  • 将库移动到 似乎可以解决问题。
  • 那么,您找到解决方案了吗?

标签: android jquery cordova jquery-mobile mobile


【解决方案1】:

前段时间我遇到了同样的问题,最后我扔掉了 phonegap 的启动器。

我建议这样做:

<script type="text/javascript" src="cordova-2.4.0.js"></script>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/jquery.mobile-1.2.0.min.js"></script>
<script type="text/javascript">
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        // your functions here
    }
</script>

希望对你有帮助

【讨论】:

  • 谢谢,我也试过了,没有,和以前一样。使用 jquery mobile 它不会触发,没有它会触发。
【解决方案2】:

试试把scripts above jquery mobile library.:

<script type="text/javascript" src="cordova-2.4.0.js"></script>
<script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
    app.initialize();
    function displayHello(){
        var name = document.getElementById("firstname").value;  
        navigator.notification.alert("My name is "+ name);  
    }
</script>
<script type="text/javascript" src="js/jquery.mobile-1.2.0.min.js"></script>

更新:

<script type="text/javascript">
    $(document).on('pageinit', function($){
        app.initialize();
    });
</script>

【讨论】:

  • 感谢您的回答,我刚试了一下,似乎没有什么影响。
  • 更新了答案,你有没有这样尝试过一次尝试$(function(){app.initialize()});
  • 我也试过这个,同样的事情。当 jQuery Mobile 脚本退出时工作正常,当我放回它时停止工作(不管它是在 app.initialize() 之前还是之后)
  • 签出最后的更新,如果这样就可以了,否则脚本可能有其他问题,看来。
  • 再次感谢,但这次使用 pageinit 且没有 jquery mobile,app.initialize() 无法运行,这是有道理的。使用 pageinit 和 jquery mobile,app.initialize() 运行,但 deviceready 永远不会触发。
【解决方案3】:

我注意到浏览器不工作,但使用模拟器或 PhoneGap 构建 ondeviciready 事件和相同的示例工作!

【讨论】:

    【解决方案4】:

    即使在将库添加到头部后,我也遇到了完全相同的问题,但是当我将 cordova.js 移动到 jquery-mobile 下时,deviceready 再次开始触发。

    <head>
    <script type="text/javascript" src="jqueryMobile/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="jqueryMobile/jquery.mobile-1.4.3.min.js"></script>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>
    </head>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-06
      相关资源
      最近更新 更多