admin管理员组文章数量:1335133
When I do:
function testtext()
{
var text = "test line 1\n\
test line 2"
;
$("#divtext").text(text);
}
It all appears on one line. When I do:
function testtext()
{
var text = "test line 1\n\
test line 2"
;
document.getElementById("divtext").innerText = text;
}
It works fine...
When I do:
function testtext()
{
var text = "test line 1\n\
test line 2"
;
$("#divtext").text(text);
}
It all appears on one line. When I do:
function testtext()
{
var text = "test line 1\n\
test line 2"
;
document.getElementById("divtext").innerText = text;
}
It works fine...
Share Improve this question asked Jul 5, 2011 at 1:08 mowwwalkermowwwalker 17.4k30 gold badges108 silver badges164 bronze badges 3- 1 what is your question?? or issue – kobe Commented Jul 5, 2011 at 1:12
-
What about if you set the
textContent
property ? – alex Commented Jul 5, 2011 at 1:12 -
2
Well a new line in HTML is created using the
<br />
element, not a "normal" line break. – Felix Kling Commented Jul 5, 2011 at 1:15
3 Answers
Reset to default 3Use $("#divtext").html(text);
Instead of $("#divtext").text(text);
If you're wondering why, it's because jQuery creates a new text node using document.createTextNode
, passing your string as the argument.
This apparently behaves differently than innerText
, though it seems to behave the same as setting .textContent
and as explicitly setting the value of a textNode through .nodeValue
or .data
.
The text() method will do an HTML escape. There are some workarounds on the API page, but perhaps the text() method isn't best suited for what you want to do?
http://api.jquery./text/
本文标签: javascriptAdding a new line to text string with jquery text() methodStack Overflow
版权声明:本文标题:javascript - Adding a new line to text string with jquery .text() method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742382784a2464426.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论