简体   繁体   English

textarea中的换行标记

[英]newline markup in textarea

   <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>

i get value from textarea like that(plain text); 我从那样的文本区域中获得了价值(纯文本); 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中获取换行标记的位置,以便可以在其中放置<br>

If I were to press enter in textarea and pass to new line then it would be easy because there would be an \\n at the place where I press enter but when I just add text and let the textarea wrap the text to a new line then I have no \\n there in the text. 如果我要在textarea中按Enter键并传递到新行,那会很容易,因为在我按Enter的地方会有一个\\n ,但是当我只添加文本并让textarea将文本换行到新行时,文字中没有\\n

I need it to be like this (not plain text) ; 我需要像这样(不是纯文本);

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 and textareaContentWithJavascript gives the same result i mean without \\n markup. 不带\\n标记的textareaContentWithJquery和textareaContentWithJavascript给出的结果相同。 So they are useless. 因此它们是无用的。

You could use &#013; 您可以使用&#013; for the new line, and then search for \\n and replace with <br> . 换行,然后搜索\\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>"));
});

Updated codepen: http://codepen.io/nobrien/pen/QNmOpv 更新的Codepen: http ://codepen.io/nobrien/pen/QNmOpv

Try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM