當我們想用document.write()輸出換行時,可能會第一時間想到加"\n",但是其實不能達到我們的想要效果,只會得到一個空格的效果。
正確的方法是使用:</br>
樣例代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<!DOCTYPE html> <html> <head> <meta charset= "utf-8" > <script> // 使用 \n 換行 ---> 錯誤的方法 document.write( "這是第一句話。" + "\n" ); // 并不會換行,只會得到一個空格的效果 document.write( "這是第二句話。" ); document.write( "</br>" ); // 正確的換行 document.write( "</br>" ); document.write( "</br>" ); // 使用正確的方法 </br> 換行 document.write( "這是第一句話。。" + "</br>" ); document.write( "這是第二句話。。" ); </script> <title></title> </head> <body> </body> </html> |
效果截圖:
到此這篇關于JavaScript用document.write()輸出換行的示例代碼的文章就介紹到這了,更多相關js document.write()輸出換行內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_43883917/article/details/109607675