我试过写一个网页
document.write("This is the first line \n And this is the second");
想要的效果是:
This is the first line
And this is the second
但是我明白了:
This is the first line And this is the second
我试着写 <br>
在里面,它起作用了。但是为什么要写转义 \n
没有?我做错了什么吗?我正在使用 Firefox。
最佳答案
你没有做错任何事。它只是 \n
字符在文本相关元素中有效,例如 alert() 和 <textarea>
.
document.write()
命令写入站点正文或显示(在 HTML 中)。替代<br>
代替 \n
在这里,因为你正在页面中编写 HTML
关于javascript -\n 换行符不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14518829/