2016-03-01 41 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,更新了我的答案。 –