将新行添加到localStorage变量中

Add new line into localStorage variable

本文关键字:变量 localStorage 新行 添加      更新时间:2023-09-26

我正在尝试向本地存储变量中添加新行,但我不知道如何做到这一点。我有这个功能:

function showHistory()
{
  if (!localStorage.history) 
  { 
      localStorage.history = ""; 
  }
        localStorage.history = localStorage.history +"something"+"XX";
        document.getElementById('history').textContent = localStorage.history;  
}

但是如果我用"''n"或"<br>"或类似的东西替换XX,如果我使用这个函数,仍然会得到像这样的输出

"something something something"

而不是

something
something
something

使用innerHTML而不是textContent为我解决了这个问题(作为新行,使用<br>):

localStorage.history = localStorage.history +"something"+"<br>";
document.getElementById('history').innerHTML = localStorage.history;