简体   繁体   English

重新加载页面时,以前的评论消失了-与本地存储有关

[英]When I reload the page my previous comments disappear - something to do with local storage

Trying to make it so that when I refresh the page the previous comments remain on the page. 尝试使之刷新页面时,以前的注释会保留在页面上。 Not quite sure how to do it. 不太清楚该怎么做。 Also was wondering how you would go about making it so that when you click on the namebox you don't have to highlight "name" and then write your name, you can just click and what you write simply replaces the "name". 也想知道如何制作它,这样当您单击名称框时,不必突出显示“名称”,然后写下您的名称,您只需单击即可,您编写的内容将简单地替换为“名称”。

//Functions for Homepage
function imgUpdate() {
    var img = document.getElementById("navImg").alt;
        if (img === "Cat Selfie") {
            document.getElementById("navImg").src = "foo.jpg";
            document.getElementById("navImg").alt = "foo"
        }
        else {
        document.getElementById("navImg").src = "cat-selfie.jpg";
        document.getElementById("navImg").alt = "Cat Selfie"
        }
}

//Functions for Comments
function clearComment(){
    $('#txt1').val(''); //short for getElement when using j query
};

function clearName(){
    $('#namebox').val('');
};

function saveComment() {
    var ctext = $('#txt1').val();
    var cname = $('#namebox').val();
    if (cname === 'Name') {cname = 'Anon';}
    alert('saveComment cname=' + cname + ' ctext=' +ctext);
    var d = Date();
    var prevComments = $('#cmtlist').html();
    var curComment='<p><span class="cmtname">'+cname+ ':' + '</span>'
        +ctext +d+' </p>'; //span = add things to something  inline
    curComment += prevComments;
        $('#cmtlist').empty();
        $('#cmtlist').append(curComment);
    clearComment();
    clearName();

    setObject('totCmts', curComment);
}



function fetchComments(){
    var inlist=getObject('totCmts');
        if(inlist === null){
            inlist='';
        }
    //display the comments
        $('#cmtlist').empty();
        $('#cmtlist').append(inlist);
}

My Html file 我的HTML文件

<html>
<head>
<meta charset="utf-8" />
<title>Dubya comments</title>
<link rel="stylesheet" href="homepage.css" type="text/css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="homepage.js"></script>
</head>
<body>
<header id="banner">
</header>
<nav>
<button type="button" onclick="clearComment()">Clear
comment</button> 
<button type="button" onclick="saveComment()">Save comment</button>
</nav>
<div id="main">
<div id="dtext">
<h4>Your comment</h4>
<input id="namebox" type="text" maxlength="32" size="20"
value="Name" />
<br />
<textarea id="txt1" class="textbox" rows="6"></textarea>
</div>
<h4>Comments</h4>
<div id="cmtlist"></div>
</div>
</body>

</html>

Jack, you can easily store data in localStorage using global object like this: 杰克,您可以使用全局对象轻松将数据存储在localStorage中,如下所示:

// your array with comments
var comments = ["First comment", "Second comment"];

// saving your comments in JSON format
window.localStorage.setItem("comments", JSON.stringify(comments));

// retrieving them
comments = JSON.parse(window.localStorage.getItem("comments"));

You can read more about localStorage on MDN 您可以在MDN上阅读有关localStorage的更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使我的页面可以关闭或重新加载,但当我再次打开页面时内容不会消失? - How do I make it so my page can close or reload but the content wont disappear when i open the page again? 当我使用 Pusher 发布内容并重新加载页面时,所有条目都会消失。 怎么修? Python Flask - When I post something with Pusher, and reload the page, all entries disappear. How to fix? Python Flask 为什么刷新页面后我在页面上的输入消失了,但是输入保存在控制台的本地存储中? - Why do my inputs on the page disappear after refreshing the page, but the inputs are saved in local storage in the console? 在 React 本地存储中添加新产品时,以前的产品消失 - The previous product disappear when adding new product in React local storage 如何使用本地存储在我的网页上保存随机结果(每次重新加载都会不同)? - How do I save a randomized result (which will be different with every reload) on my webpage with local storage? 如何根据本地存储中的变量快速排序? - How do I quicksort something based on its variable in local storage? 当页面重新加载时,getLoginStatus javascript facebook消失 - getLoginStatus javascript facebook disappear when page reload 本地存储在页面重新加载时不断重置 - Local Storage Keeps Resetting On Page Reload JWT 本地存储,Cookies 和页面重新加载 - JWT Local storage , Cookies and Page Reload 如何让我的 Pixlee Instagram 提要在 NextJS 中的页面重新加载时不消失? - How can I get my Pixlee Instagram feed to not disappear on page reload in NextJS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM