开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜