在使用 JQueryMobile 和 IBM Worklight 6.0 时找不到 $ -

Posted

技术标签:

【中文标题】在使用 JQueryMobile 和 IBM Worklight 6.0 时找不到 $ -【英文标题】:can't find $ - while using JQueryMobile and IBM Worklight 6.0 【发布时间】:2013-11-19 08:22:03 【问题描述】:

我正在尝试将条形码扫描仪集成到在 worklight 上开发的电话间隙应用程序中,当前的 html 页面来自这样的主页中的 href。

<a data-role="button" href="itemscan.html" rel="external" data-shadow="false"
    data-theme="none"><img src="images/dashboard_barcode.png"  ></a>

我的html页面:

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
-->
<html>
<head>
<title>BarCode Scanner</title>
            <link rel="stylesheet" href="jqueryMobile/jquery/jquery.mobile-1.3.2.css">
            <script src="jqueryMobile/jquery/jquery.mobile-1.3.2.js"></script>


</head>
    <body>
        <div class="app">
            <h1>Zxing Scanner!!</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
                <p id="barcoderesult">Bar Code Result: Nil</p>

            </div>
        </div>
        <script type="text/javascript" src="js/cordova-2.5.0.js"></script>
        <script type="text/javascript" src="modules/core/CoreFunc.js"></script>
        <script type="text/javascript" src="js/barcodescanner.js"></script>
        <script type="text/javascript" src="js/itemscan.js"></script>
        <script type="text/javascript" src="js/MenuPanel.js"></script>
        <script type="text/javascript">
            app.initialize();
       </script>
    </body>
</html>

JS 文件:

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
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');
        // demo the scan
        console.log('about to scan');
        try 
            var scanned = app.scan();
            console.log('scan triggered', scanned);
         catch (e) 
            console.log('scan failed');
            console.log(JSON.stringify(e));
            console.log('that sucks... reloading in 10');
            setTimeout(function() 
                console.log('reloading now...');
                app.onDeviceReady();
            , 10000);
        
    ,
    // 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);
    ,
    /**
     * here is an example for scanning a barcode...
     * obviously, your own JS logic <<here>>
     *
     * Note the require() method is called on window.cordova
     *   this is different than the readme!
     */
    scan: function() 

        console.log('scan(): init');
        // documentation said the syntax was this:
        // var scanner = window.PhoneGap.require("cordova/plugin/BarcodeScanner");
        // but playing with options, seems like it should be this:
        var scanner = window.cordova.require("cordova/plugin/BarcodeScanner");
        scanner.scan(
                function (result) 

                    var parentElement = document.getElementById("barcoderesult");
                    parentElement.innerHTML = result.text;
//                    alert("We got a barcode\n" +
//                        "Result: " + result.text + "\n" +
//                        "Format: " + result.format + "\n" +
//                        "Cancelled: " + result.cancelled);
                    console.log("We got a barcode\n" +
                            "Result: " + result.text + "\n" +
                            "Format: " + result.format + "\n" +
                            "Cancelled: " + result.cancelled);
                  $.mobile.changePage( "../itemDetails.html",  transition: "slideup", changeHash: false );

                ,
                function (error) 
                    alert("Scanning failed: " + error);
                
                );
    
;

扫描工作正常,成功回调后我得到条形码,然后我得到这个,我无法更改页面。

[LOG] 错误回调错误:BarcodeScanner957715450 = ReferenceError:找不到变量:$

从我所做的所有搜索中,我发现 jQuery 没有正确加载。我对所有这些网络技术有点陌生,因此感谢您提供任何帮助。

【问题讨论】:

jQuery.js 未在 itemscan.html 的head 中加载 @Omar 那么怎么办呢? 只需将它添加到头部的 JQM 之前,那里有 JQM js 和 CSS 文件。 【参考方案1】:

Can't find variable: $ jquery 未加载到您的代码中。请确保 jquery 正确加载,并且没有如 Gamex 所述的 jquery 冲突。

window.onload = function() 
    if (window.jQuery)         
    alert("jQuery is loaded  !");
     else         
    alert("jQuery is not loaded");
    

【讨论】:

你的 jqury 与另一个版本冲突,所以我想请求你把你的 js 放在上面,并且在你的页面上只包含一个 jquery 库 我尝试添加这个语句 if($.mobile) alert ('Jquery loaded'); 在初始化函数中测试 JQuery 是否正确加载但没有任何效果。 您能否说明如何确保 Jquery 正确加载? 我收到警告“jQuery 未加载”。

以上是关于在使用 JQueryMobile 和 IBM Worklight 6.0 时找不到 $ -的主要内容,如果未能解决你的问题,请参考以下文章

IBM Worklight 6.0 - .war 文件部署失败

jQuery Mobile UI 无法与 IBM Worklight 一起正常工作

IBM Worklight 中的登录认证

IBM Worklight 6.0.0.1 - JavaScript 未在 jQuery Mobile 多页应用程序中执行

IBM Worklight 应用程序无法在 Xcode 中运行

IBM Worklight 6.1 - 富页面编辑器在进行更改后不刷新