admin管理员组文章数量:1335434
I have the following code:-
function data(x) {
var dataList = "";
for (var key in obj) {
dataList += key + ": " + obj[key] + "\n"
}
$('#modalText').text(dataList);
document.getElementById('dataModal').style.display='block';
}
And it populates the HTML like this:
<p id="dataList"></p>
Although I have a '\n' concatenated to my string with each iteration of the loop, I cannot get the line break after each key/value pair to show in my modal. I have also tried the jQuery .append() function instead of .text(), to no avail.
My variable, dataList, prints in the console with proper line breaks, like this:
key: 123
time: 12:00
department: taxes
But when printed in the modal, there are no line breaks:
key: 123 time: 12:00 department: taxes
How can I get it to show in my modal with a new line break after each key-value pair, as it shows in the console?
I have the following code:-
function data(x) {
var dataList = "";
for (var key in obj) {
dataList += key + ": " + obj[key] + "\n"
}
$('#modalText').text(dataList);
document.getElementById('dataModal').style.display='block';
}
And it populates the HTML like this:
<p id="dataList"></p>
Although I have a '\n' concatenated to my string with each iteration of the loop, I cannot get the line break after each key/value pair to show in my modal. I have also tried the jQuery .append() function instead of .text(), to no avail.
My variable, dataList, prints in the console with proper line breaks, like this:
key: 123
time: 12:00
department: taxes
But when printed in the modal, there are no line breaks:
key: 123 time: 12:00 department: taxes
How can I get it to show in my modal with a new line break after each key-value pair, as it shows in the console?
Share Improve this question edited Jul 24, 2017 at 18:40 Death-is-the-real-truth 72.3k10 gold badges57 silver badges104 bronze badges asked Jul 24, 2017 at 18:19 bobbob 6395 silver badges25 bronze badges 2- Try typing a line break into the raw HTML; you'll see that it doesn't show up when shown in a browser. Same deal when added via script. – Heretic Monkey Commented Jul 24, 2017 at 18:25
- 1 In a modal, there won't be a line break via a newline character -- the contents of a modal (unless it's a textarea) will be pure HTML and newlines aren't. Is there a reason you aren't using .html() instead of .text() ? – Snowmonkey Commented Jul 24, 2017 at 18:26
2 Answers
Reset to default 7Whitespace means nothing in HTML. You need to either use <br/>
and set the .html()
, or you need to add a white-space: pre
to your dataList <p>
tag. Using pre
or pre-wrap
will respect whitespace (and therefore \n characters)
simply <br>
is sufficient to get line break rather "/n" as shown in below code.
function data(x) {
var dataList = "";
for (var key in obj) {
dataList += key + ": " + obj[key] + "<br>"
}
$('#modalText').text(dataList);
document.getElementById('dataModal').style.display='block';
}
本文标签: javascriptLine break not working with jQuery text()Stack Overflow
版权声明:本文标题:javascript - Line break not working with jQuery .text() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742378621a2463663.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论