首页 > 解决方案 > 保存 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.

希望有人可以指导我如何解决问题。谢谢。

标签: javascripthtmltextarea

解决方案


推荐阅读