Html 在innerHTML中添加一个“新行”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19438895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add a "new line" in innerHTML
提问by Gudron Swiss
I am trying to create a table with images in first cell and information about the pic in second cell.
我正在尝试创建一个表格,其中第一个单元格中包含图像,第二个单元格中包含有关图片的信息。
I need to add different information in one cell, like that:
我需要在一个单元格中添加不同的信息,如下所示:
cellTwo.innerHTML = arr_title[element] + arr_tags[element];
Is it possible to add a "new line" there? I mean like that:
是否可以在那里添加“新行”?我的意思是这样的:
cellTwo.innerHTML = arr_title[element] + "/n" + arr_tags[element];
回答by Juan Mendes
The simplest way is by adding a line break as html
最简单的方法是将换行符添加为 html
cellTwo.innerHTML = arr_title[element] + "<br />" + arr_tags[element];
If you want your newlines to be treated literally, you could use the <pre>
tag
如果您希望按字面意思处理换行符,则可以使用<pre>
标签
cellTwo.innerHTML =
"<pre>" + arr_title[element] + "\n" + arr_tags[element] + "</pre>";
回答by cbur
To round out your understanding:
为了完善您的理解:
Since it is html (innerHTML
)it renders html and you can use any html you wish, so in this case simply add an good old fashioned <br>
:
因为它是 html ( innerHTML
)它呈现 html 并且你可以使用任何你想要的 html,所以在这种情况下只需添加一个好的老式<br>
:
var test = document.getElementById('someElementId');
test.innerHTML = "The answer <br>to life, the universe, and everything...<br> is 42.";
If it were a string, such as in an alert box or text box etc. then /n would be correct:
如果它是一个字符串,例如在警告框或文本框等中,那么 /n 将是正确的:
alert('Never /n Forget your towel.');
Happy Coding!
- $cr1ptN!nj@
快乐编码!
- $cr1ptN!nj@
回答by Parnell
No, <br />
does not work in asp .net but you can instead write it like so
不,<br />
在 asp .net 中不起作用,但您可以这样编写
cellTwo.innerHTML = arr_title[element] + arr_tags[element]; arr_title[element] + "/n" + arr_tags[element];
Edit - alternative wrapped in code tags
编辑 - 替代包装在代码标签中
cellTwo.innerHTML = arr_title[element] + arr_tags[element];
cellTwo.innerHTML += arr_title[element] + "/n" + arr_tags[element];
Semicolon ";" seems to act as line breaks Remember the "+=" to assign multiple values to the string
分号“;” 似乎充当换行符 记住“+=”为字符串分配多个值