javascript - 在 json 中存储具有换行符的字符串时出现问题
问题描述
我有一个文本框,我将其值存储在 json 中,如下所示
description=JSON.stringify($("#textarea").val());
然后我将它存储在一个json对象中,如下所示
myObj['description']=description;
整个 Json 现在存储在我的 mysql 数据库中。每当我尝试获取整个 json 并使用描述重新填充文本区域时我面临的问题是我得到以下字符串
Your Name Is ABC\nYou Live in\nYour Phone number Is\n
基本上现在我的文本区域无法将 \n 识别为换行符
解决方案
您可以使用 jquery 设置 html 内容,例如
$(document).ready(function(){
$("button").click(function(){
$("#loading-area").html('Your Name Is ABC\nYou Live in\nYour Phone number Is\n').wrap('<pre />');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<body>
<button>Change content of all p elements</button>
<p id="loading-area">This is the loading para.</p>