简体   繁体   English

如何在不使用“style = white-space:pre”的情况下使用新行创建文本节点?

[英]How to make text nodes with new lines in it without using “style=white-space: pre”?

I'm trying to get a textnode with new lines in it (my text contains \\n and x0a ). 我正在尝试使用新行的textnode(我的文本包含\\nx0a )。 I found the way to do this, using white-space: pre , and it works, but the problem is that the content doesn't fit to the parent div, I got the new lines in the text, but when the line is long, it goes beyond the window. 我找到了这样做的方法,使用white-space: pre ,它可以工作,但问题是内容不适合父div,我在文本中得到了新行,但是当行很长时,它超越了窗口。

This is the code: 这是代码:

var textdata = document.createTextNode(text_with_multiples_lines);
PanelBody.style = "white-space: pre;"
PanelBody.appendChild(textdata);

截图

Somebody know how to make new lines inside a textnode without exceeding the window size? 有人知道如何在不超出窗口大小的情况下在textnode中创建新行吗?

Set white-space to pre-wrap instead. white-space设置为pre-wrap

This also enables text lines to break as needed to fill the element box :) 这也可以根据需要打开文本行来填充元素框:)

More on that: MDN - white-space 更多内容: MDN - 白色空间

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

相关问题 使用 Simple JavaScript 计算带空格和不带空格的文本区域字符 - Count text area characters with space and without white-space using Simple JavaScript 空白:在 Vue 中将文本作为道具传递时,预换行不起作用 - white-space: pre-wrap doesn't work while passing text as a prop in Vue 将文本跨越到 div 的边缘(无空格) - Span text to the edge of the div (no white-space) 用白色空间美化PRE中的亮点:nowrap是IE中的一行 - prettify highlightning in a PRE with white-space:nowrap is one line in IE 如何在此jQuery Mobile网格按钮文本中添加空白:正常? - How do I add white-space:normal in this jQuery Mobile grid button text? Javascript:检查用户的输入文本是否为“空白语言” - Javascript: check if the user's input text is “white-space language” “white-space:pre;”根据W3C规范,CSS样式似乎不起作用 <textarea> HTML元素 - 它总是包装,而不仅仅是换行 - “white-space: pre;” CSS style does not seem to work according to W3C spec in <textarea> HTML element - it always wraps, not just on newlines 过渡无效(空白:nowrap-&gt;空白:正常) - Transition not working (white-space: nowrap -> white-space: normal) 使用“white-space:nowrap”会导致div幻灯片显示问题 - Using “white-space: nowrap” causes issue with div slideshow 删除不必要的空白区域 - Removing unnecessary white-space
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM