0
<html>
    <head>
        <script type="text/javascript"  src="js/cordova.js"></script>
        <script type="text/javascript" >
            function init() {

                document.addEventListener("deviceready", onDeviceReady, false);
            }

            function populateDB(tx) {
                tx.executeSql('DROP TABLE IF EXISTS DEMO');
                tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
                tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
                tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
            }

            function queryDB(tx) {
                tx.executeSql('SELECT * FROM DEMO', [], querySuccess, errorCB);
            }
            function querySuccess(tx, results) {
                var len = results.rows.length;
                alert("DEMO table: " + len + " rows found.");
                for (var i = 0; i < len; i++) {
                    alert("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " + results.rows.item(i).data);
                }
            }


            function errorCB(err) {
                alert("Error processing SQL: " + err.code);
            }

            function successCB() {
                var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
                db.transaction(queryDB, errorCB);
            }


            function onDeviceReady() {
                var db = window.openDatabase("Database", "1.0", "Cordova Demo", 200000);
                db.transaction(populateDB, errorCB, successCB);

            }

        </script>


    </head>
    <body onload="init();" >
        <a  onclick="onDeviceReady();"></a>


    </body>

</html>

我运行上面的代码以在 Android 选项卡上保存数据(我从电话间隙网站获取该代码)。但是当我尝试上面的代码时,我得到以下错误。我在 Android 选项卡上运行上面的代码。你能帮我解决这个问题吗.

0-29 16:27:57.955: E/Web Console(3813): Uncaught ReferenceError: onDeviceReady is not    defined at file:///android_asset/www/index.html:1
4

1 回答 1

0

移动<script>到身体。如果它在 head 中,则不会在 body 加载之前进行评估。

http://jsfiddle.net/KYknS/4/

于 2013-10-29T06:06:43.180 回答