gpt4 book ai didi

javascript - 文本区域中的换行标记

转载 作者:行者123 更新时间:2023-12-03 07:15:13 26 4
gpt4 key购买 nike

   <script>
$('#textarea_').on('keyup', function () {
var textareaContentWithJquery = $('#textarea_').val();
var textareaContentWithJavascript = document.getElementById('textarea_').value;
});
</script>

<textarea cols="20" id="textarea_" name="textarea_" rows="2">
Some text in textarea here is created without pressing enter,
just plain text. But how can i get the position
of newline markup from textarea so that i can put <br> there?
</textarea>

我从文本区域获取值(纯文本);这里的文本区域中的一些文本是在不按 Enter 键的情况下创建的,只是纯文本。但是我如何从文本区域获取换行标记的位置,以便我可以放置 <br>在那里?

如果我在文本区域中按 Enter 并传递到新行,那么这会很容易,因为会有 \n在我按 Enter 的地方,但当我只是添加文本并让文本区域将文本换行时,我没有 \n文中就有。

我需要它像这样(不是纯文本);

Some text in textarea here is created without pressing enter,\n
just plain text. But how can i get the position\n
of newline markup from textarea so that i can put <br> there?\n

So i would easily replace \n with <br>
Some text in textarea here is created without pressing enter,<br>
just plain text. But how can i get the position<br>
of newline markup from textarea so that i can put <br> there? <br>

textareaContentWithJquery 和 textareaContentWithJavascript 给出相同的结果,我的意思是没有 \n标记。所以它们毫无用处。

最佳答案

您可以使用&#013;寻找新行,然后搜索 \n并替换为 <br> .

 <textarea cols="20" id="textarea_" name="textarea_" rows="12" cols="150">
Some text in textarea here is created without pressing enter, just plain text.
But how can i get the position of newline markup from textarea so that i can put <br>&#013; there?
</textarea>

$('#textarea_').on('keyup', function () {
var textareaContentWithJquery = $('#textarea_').val();
console.log(textareaContentWithJquery.replace(/\n/g, "<br>"));
});

更新的代码笔:http://codepen.io/nobrien/pen/QNmOpv

关于javascript - 文本区域中的换行标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36454734/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com