开发者

Javascript: Textarea to html, how to save exact whitespaces count in html?

开发者 https://www.devze.com 2023-02-14 03:40 出处:网络
How to save exact whitespace count between words from textarea when putting it\'s value into html? &开发者_运维问答lt;textarea>word \"10 whitespaces here\" word \"20 whitespaces here\" word</te

How to save exact whitespace count between words from textarea when putting it's value into html?

&开发者_运维问答lt;textarea>word "10 whitespaces here" word "20 whitespaces here" word</textarea>

html:

<div style="width:50px;overflow:hidden;">word "10 whitespaces here" word "20 whitespaces here" word</div>

So that all text inside div would be visible.


Assuming you are creating your div from scratch, here is a way to replace the spaces with the HTML-friendly &nbsp;.

var newDiv = document.createElement("div");
// the below replaces two spaces with "&nbsp; "
newDiv.innerHTML = value.replace(/  /g, " &nbsp;");


Use CSS property white-space: pre or replace spaces with &nbsp;.

0

精彩评论

暂无评论...
验证码 换一张
取 消