📜  将 div 的文本保存到 localStorage,文本更改时更新 localStorage - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:17.561000             🧑  作者: Mango

代码示例1
// get the text
var text = $('#test').text();

// set the item in localStorage
localStorage.setItem('test', text);

// bind text to 'blur' event for div
$('#test').on('blur', function() {

    // check the new text
    var newText = $(this).text();

    // overwrite the old text
    localStorage.setItem('test', newText);

    // test if it works
    alert(localStorage.getItem('test'));

});