javascript - 保存 textarea 值和格式
问题描述
我在保存 textarea 值和格式时遇到问题。保存时的值和格式不一样。
以下是我的示例编码:
<script>
function save()
{
TestVar = document.getElementById("text2").value;
mydoc = document.open();
mydoc.write(TestVar);
}
</script>
</head>
<body>
<form id=formtest>
<textarea name="textarea" id="text2"></textarea>
<input type="button" value="save" onclick="save()">
</form>
</body>
例如,如果我在 textarea 中输入单词是:
Once there lived a greedy man in a small town.
He was very rich, and he loved gold and all things fancy.
然后,当我单击“保存”按钮时,会显示结果,它无法遵循我在 textarea 中输入的新行格式:
Once there lived a greedy man in a small town. He was very rich, and he loved gold and all things fancy.
实际上,我想要如下图所示的预期结果:
Once there lived a greedy man in a small town.
He was very rich, and he loved gold and all things fancy.
希望有人可以指导我如何解决问题。谢谢。
解决方案
推荐阅读
- ios - HealthKit requestAuthorization for correlationType(forIdentifier: .bloodPressure)
- python - 从 html 中抓取表格(并且 ID 方法不起作用)
- android - 在侦听器中获取 Kochava 归因错误
- java - Visual Studio Code,Java 扩展,如何将 jar 添加到类路径
- python-2.7 - 在python变量中读取和存储xml数据
- multithreading - 在混合应用程序 C++ /cli 中使用线程从托管类调用本机类中的本机函数
- node.js - 使用 Mongoose 和 graphql-yoga 实现分页
- python-3.x - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 292-293: truncated \UXXXXXXXX escape during import
- angular - 如何在角度 4 中使用两个相同的路径 ='' 路由应用程序
- react-native - 在本机反应中将样式设置为导航选项内的自定义组件