javascript -\n 换行符不起作用

标签 javascript

我试过写一个网页

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/

相关文章:

javascript - Laravel 5 Javascript 启用/禁用检测

javascript - 在函数原型(prototype)中使用 _.extend

javascript - jQuery slideToggle 在关闭时跳转

javascript - 判断div是否滚动到底部

javascript - 如何删除 OpenLayers-Map 中的标准控件?

javascript - HighCharts - 导出图表时忽略 setTitle()

javascript - gulp-svg-sprites 输出 [Object object] 而不是图标

javascript - 如何在循环中增加setInterval计时器

javascript - 遍历所有图像并更改大小?

javascript - 加快 WordPress 网站上的轮播速度