android - jQuery Mobile 中断 Phonegap 设备就绪事件

标签 android jquery cordova jquery-mobile mobile

所以我已经让 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>

索引.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 的启动器。

我建议这样做:

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

希望对你有帮助

关于android - jQuery Mobile 中断 Phonegap 设备就绪事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14976595/

相关文章:

ios - PhoneGap 安全吗?

java - Android Listview 不滚动仍然无法正常工作

android - 在android 4.0下用另一个按钮替换一个按钮

c# - 点击事件中的 ajax 请求后重定向

Cordova 插件从 UI 线程回调到 javascript 似乎在 PhoneGap 3.3 中被破坏

java - Cordova Phonegap 2.2.0 视频播放器插件

android - 设计自定义 Android ViewGroup 子类

android - Android 会在屏幕关闭时降低下载速率吗?

javascript - jQuery Else/If 阻止继承样式

javascript - 通过 JavaScript 清除 HTML 文件上传字段