【问题标题】:Store in LocalStore an element added by jquery via append()在 LocalStore 中存储 jquery 通过 append() 添加的元素
【发布时间】:2014-09-19 17:33:01
【问题描述】:

我想在 LocalStore 中存储 jquery 通过append() 添加的元素。我使用Storage.js 来存储数据,但刷新后,项目不会存储在LocalStore 中。我忘记了什么?

HTML

<a href="#f" id="i_">→1</a><br>
<a href="#f" id="i_">→2</a><br>
<a href="#f" id="i_">→3</a>

<input id="add" type="button" value="Add">

JS

$("#add").bind("click", function () {
 var id = $(this).prev().attr('data-key');
 var number = id.substring(6, 8);
 $("#add").before('<br><a href="#f" id="i_" data-orig-text="→' + number + '" data-key="' + id + '" contenteditable class>→' + number + '</a>');
});

$('a').storage({storageKey:'Links'});

这里有一个演示:http://jsfiddle.net/DgV78/20/

【问题讨论】:

    标签: jquery local-storage storage


    【解决方案1】:

    与其使用 Storage.js,不如使用 HTML5 LocalStorage,这里是full documentation。如下所示:

    要将数据保存到本地存储中:

    if(typeof(Storage) !== "undefined") {
        // Code for localStorage/sessionStorage.
        var saved_html = "";
        $("a").each(function(){
           saved_html += $(this).html();
        });
        localStorage.setItem("saved_html", saved_html);
    } else {
        // Sorry! No Web Storage support..
    }
    

    获取数据:

    $("body").html(localStorage.getItem("saved_html")); 
    

    注意:我无法在您的小提琴上试用它,因为它缺少 data-key..

    【讨论】:

      猜你喜欢
      • 2012-10-01
      • 2013-12-02
      • 2012-01-12
      • 2011-05-09
      • 2011-01-10
      • 1970-01-01
      • 2010-12-23
      • 2012-06-04
      • 1970-01-01
      相关资源
      最近更新 更多