Jquery 在本地存储上保存换行符

Jquery 在本地存储上保存换行符,jquery,local-storage,line-breaks,Jquery,Local Storage,Line Breaks,所以我试着用localstorage做一个简单的便笺,在这里你可以自动输入并保存 我每次按linebreak都会遇到问题。Localstorage不应用换行符 HTML: 例如: 尝试键入内容,按return并键入更多内容。然后刷新页面 如何将/换行符应用于本地存储?我想我得到了你的问题 在Ajax调用中保存数据 像这样传递值 if u use $('#container').html(); // You ll get html tags with text //like th

所以我试着用localstorage做一个简单的便笺,在这里你可以自动输入并保存

我每次按linebreak都会遇到问题。Localstorage不应用换行符

HTML:

例如:

尝试键入内容,按return并键入更多内容。然后刷新页面


如何将

/
换行符
应用于本地存储?

我想我得到了你的问题

在Ajax调用中保存数据

像这样传递值

if u use 
    $('#container').html(); // You ll get html tags with text
    //like this-----------> teste<br><br><br>r
else
    $('#container').text(); //  You ll get only  text
    //like this -----------> tester

try console and check buddy,

console.log('----------->'+$('#container').html());
console.log('----------->'+$('#container').text());


Your coding:

$('div.s_content').text(localStorage.getItem ("foo"))


$("div.s_content").on("keyup paste", function () {
    localStorage.setItem ("foo", $('div.s_content').html()) // changes has to Here text to html
});
如果您使用
$('#container').html();//您将获得带有文本的html标记
//像这样-----------测试


r 其他的 $(“#容器”).text();//你只会收到短信 //像这样------------>测试仪 尝试控制台并检查buddy, console.log('----------->'+$('#container').html()); console.log('----------->'+$('#容器').text()); 您的编码: $('div.s_content').text(localStorage.getItem(“foo”)) $(“div.s_content”)。关于(“键控粘贴”,函数(){ localStorage.setItem(“foo”,$('div.s_content').html())//必须将此处的文本更改为html });
将两个.text()调用都更改为.html(),它应该可以工作:

$('div.s_content').html(localStorage.getItem ("foo"))

$("div.s_content").on("keyup paste", function () {
   localStorage.setItem ("foo", $('div.s_content').html());
});​


因为您使用的是一个标记,所以其中的数据是HTML而不仅仅是文本。因此,您需要将div.s_content的内容设置为带有br或div标记的HTML,而不仅仅是换行符。如果要从其他地方提取文本,首先需要将换行符转换为br或div标记。

为什么我没有想到这一点。谢谢,这就是答案。由于斯洛莱夫用同样的答案回答得更快,我选择了他的答案作为正确答案。谢谢
if u use 
    $('#container').html(); // You ll get html tags with text
    //like this-----------> teste<br><br><br>r
else
    $('#container').text(); //  You ll get only  text
    //like this -----------> tester

try console and check buddy,

console.log('----------->'+$('#container').html());
console.log('----------->'+$('#container').text());


Your coding:

$('div.s_content').text(localStorage.getItem ("foo"))


$("div.s_content").on("keyup paste", function () {
    localStorage.setItem ("foo", $('div.s_content').html()) // changes has to Here text to html
});
$('div.s_content').html(localStorage.getItem ("foo"))

$("div.s_content").on("keyup paste", function () {
   localStorage.setItem ("foo", $('div.s_content').html());
});​