簡體   English   中英

HTML 5 / Javascript中的本地存儲問題

[英]Local storage issue in HTML 5 / Javascript

我正在使用Phonegap制作清單應用程序,但是由於它們都使用Web技術,因此它也在瀏覽器中進行調試。 無論如何,我在使用HTML 5本地存儲時遇到問題。 也許我的代碼被破壞了,或者我使用了錯誤的代碼。 每當我刷新頁面或退出/返回時,數據都消失了。

感謝任何幫助:^)

我在我的實時域和本地域中都對其進行了測試,但兩者均無法正常工作。

<!DOCTYPE html>

<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="msapplication-tap-highlight" content="no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <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" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>Hello World</title>

        <script>
            $(function() {
              var arr = [];
              var i;
              $("button").click(function() {
                arr.push($(":text").val());
                $(":text").val("");
                localStorage.setItem("item" + arr.length, arr[arr.length - 1]);
                $("#x").append(localStorage.getItem("item" + arr.length) + "<br>");
              })
            })
        </script>
    </head>
    <body>
        <div class="app">
            <h1>Alex</h1>
            <input type="text" id="item" placeholder="Enter an item">
            <button id="add">Add Item</button>
            <br>
            <div id="x"></div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            app.initialize();
        </script>
    </body>
</html>

如前所述-數據被持久保存並正確保存。

但是,邏輯不正確並且會覆蓋localStorage中的所有元素(第一個元素覆蓋第一個元素,第二個元素覆蓋前兩個元素,第三個元素覆蓋前三個元素,依此類推)。 也許這就是掩蓋問題的原因? :)

坦率地說,我不執行循環運行並用最后一項覆蓋所有現有邏輯(每次將其寫入屏幕上的列表)的邏輯...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM