[英]Overflow word wrap with textContent
I am trying to insert a long text into HTML via Javascript. 我正在尝试通过Javascript将长文本插入HTML。 Here is my code. 这是我的代码。
function introStory(){
var txt = document.getElementById("paragraph");
txt.setAttribute('style', 'white-space: pre;');
txt.textContent = ""
txt.textContent += "\"Kill them all!\"\r\n\r\nThey came without warning. The humans, beasts, and holy hosts attacks my village and family. They slaughtered all my neighbors, killed my mother and my father."
}
For CSS: 对于CSS:
#paragraph{
overflow: hidden;
border: 1px solid rgba(255,255,255,0.7);
text-align: left;
word-wrap: break-word;
overflow-wrap: break-word;
}
The text inserted into the div named paragraph doesn't break word as I want it to be. 插入div命名段落中的文本不会像我希望的那样打断单词。 It continues to outside of the designated div. 它继续到指定的div之外。 Am I doing something wrong here? 我在这里做错什么了吗?
Use white-space: pre-wrap;
使用white-space: pre-wrap;
or white-space: pre-line;
或white-space: pre-line;
instead of white-space: pre;
而不是white-space: pre;
Stack snippet 堆栈片段
function introStory(){ var txt = document.getElementById("paragraph"); txt.setAttribute('style', 'white-space: pre-wrap;'); txt.textContent = "" txt.textContent += "\\"Kill them all!\\"\\r\\n\\r\\nThey came without warning. The humans, beasts, and holy hosts attacks my village and family. They slaughtered all my neighbors, killed my mother and my father." } introStory();
#paragraph{ overflow: hidden; border: 1px solid rgba(255,255,255,0.7); text-align: left; }
<div id="paragraph"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.