簡體   English   中英

JavaScript換行問題

[英]JavaScript line break issue

在此片段中,當寫入'FirstText'時,將跳過第一行的其余部分。 然后'SecText'寫在第二行:

<pre>
 <script>
  function text(){
  document.write("FirstText \n SecText");
  }
  text();
 </script>
</pre>

但是當我在這個函數上使用setInterval()時,單詞會彼此相鄰寫入(不會跳過行)。

有什么建議么?

您正在看到\\n創建一個新行,因為您在<pre />標記內; 通常你必須使用<br />來查看<pre />之外的類似新行。

在頁面加載完成之前調用document.write ,輸出就會輸入到位; 所以你會在<pre />看到你的FirstText \\n SecText

然而,當頁面加載 (內它被稱為setInterval ),現有的頁面結果寫入之前清除; 因此<pre />正被刪除,你再也看不到你的新行了。

如果你還沒有關閉document使用document.close()連續調用document.writesetInterval要添加到通過的第一次迭代打開的文檔流setInterval

你可以用<br />而不是\\n來解決這個問題。

     <script>
      function text(){
      document.write("FirstText <br /> SecText <br />");
      }

      setInterval(text, 1000);
     </script>

有關詳細信息,請參閱https://developer.mozilla.org/en-US/docs/Web/API/document.writedocument.write清除頁面

嘗試這個:

document.write("FirstText <br /> SecText <br/>");

工作實例:

http://jsbin.com/oyolod/4/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM