简体   繁体   English

使用本地存储刷新页面后保留跨度和新创建的元素

[英]Keep span and newly created elements after page refresh using local storage

I'm making a comment page using only javascript and no JQuery but I don't know how to go about keeping the comments on the page after it's refreshed. 我仅使用JavaScript而不使用JQuery制作评论页面,但不知道刷新后如何在页面上保留评论。 I've stored every comment in an array and that array in local storage but I don't know how to make them appear in the span after the refresh. 我已经将每个注释存储在一个数组中,并且将该数组存储在本地存储中,但是我不知道如何使它们在刷新后显示在范围中。 Also, I was wondering if I could just store the whole span in local storage and onload put it back there? 另外,我想知道是否可以将整个跨度存储在本地存储中,然后onload放回本地?

<script>

var listofcomments = []

function action(){
    var comment = document.getElementById('header').value;
    listofcomments.push(comment);
    localStorage.setItem('commentstorage',listofcomments);
    document.getElementById('header').value = " ";
    var username = 'username:'
    var para = document.createElement('P');
    para.innerHTML= username + comment + '<br>';
    document.getElementById('hello').appendChild(para);

 }



</script>


<textarea id='header' type='text' rows='6' cols='100' name='server'>
</textarea>
<input onclick="action(); " type="button" value='Comment'>
<span id='hello'>

</span>

I think that would be proper. 我认为那是适当的。 If you store all the data in local storage and at onload you restore data from local storage. 如果将所有数据存储在本地存储中,并且在加载时从本地存储还原数据。

var listofcomments = localStorage.getItem('commentstorage');

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM