admin管理员组文章数量:1401233
I have a String with \n
- line breaks in Javascript, that I want to replace for another text using createTextNode()
in Javascript.
<script type="text/javascript">
function changeText() {
var Explanation="Test\n Test2";
var parent3 = document.getElementById("The_Explanation");
parent3.innerHTML='';
var myP3 = document.createElement("p");
var myText3 = document.createTextNode(""+Explanation);
myP3.appendChild(myText3);
parent3.appendChild(myP3);
}
</script>
All tries to get the line break into the new text failed so far. All functions that replace \n
to <br>
only bring me to the result
"Test<br> Test2"
Any ideas anyone?
I have a String with \n
- line breaks in Javascript, that I want to replace for another text using createTextNode()
in Javascript.
<script type="text/javascript">
function changeText() {
var Explanation="Test\n Test2";
var parent3 = document.getElementById("The_Explanation");
parent3.innerHTML='';
var myP3 = document.createElement("p");
var myText3 = document.createTextNode(""+Explanation);
myP3.appendChild(myText3);
parent3.appendChild(myP3);
}
</script>
All tries to get the line break into the new text failed so far. All functions that replace \n
to <br>
only bring me to the result
"Test<br> Test2"
Any ideas anyone?
Share Improve this question edited Nov 22, 2012 at 11:34 George 36.8k9 gold badges69 silver badges109 bronze badges asked Nov 22, 2012 at 11:28 MrWeixMrWeix 3771 gold badge6 silver badges18 bronze badges 02 Answers
Reset to default 8You can't put an element node inside a text node. Text nodes cannot have child nodes.
- Split your string on new lines
- Loop over the array and generate a text node for each part
- Use
createElement('br')
to create a line break to go between each text node - append all of the above
You can use replace for that:
Explanation.replace( /\n/g, "<br />" )l
本文标签: line breaksjavascript Replacing n with ltbrgt inside createTextNodeStack Overflow
版权声明:本文标题:line breaks - javascript Replacing n with <br> inside createTextNode - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744191896a2594556.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论