2016-03-01 44 views
1

如何在JavaScript中編寫"<br>"?我不想讓它突破並進入下一行,我只想寫html標籤。例如:「html中的中斷標記是...」JavaScript document.write br

下面是我想要的示例。

<script type = "text/javascript"> 
    document.write('"<br>"'); 
</script> 
+2

不要使用'document.write',請參閱[規範]警告(http://www.w3.org/TR/html5/webappapis。 HTML文件撰寫#%28%29)。改用DOM方法。 – Oriol

+0

在document.write()中,您可以像使用實體一樣直接將其放入HTML中,然後執行此操作。 – Barmar

回答

1

只需使用HTML entities

Character Entity Note 
--------- ------- ---------------------------------------------------------------- 
    &  &amp; Character indicates the beginning of an entity. 
    <  &lt;  Character indicates the beginning of a tag 
    >  &gt;  Character indicates the ending of a tag 
    "  &quote; Character indicates the beginning and end of an attribute value. 

document.write('The break tag in html is &lt;br&gt;.');

0

你可以使用的<> HTML實體:

< : &lt; 
> : &gt; 

&lt;br&gt; = <br> 

Hoep這會有所幫助。


document.write("I don't want &lt;br&gt; to break and go to the next line");

+1

這些是HTML實體,而不是ASCII碼。 – Barmar

+0

感謝@Barmar,更新了我的答案。 –