如何使用document.write()将标签写为字符串?

时间:2014-09-09 00:05:26

标签: javascript

是否可以使用document.write()将标记写为字符串?

var myWindow= window.open('', '', 'width=500, height=500');
document.write("<h1> Hello! </h1>"

这会写你好!但我想要的输出是 - &gt;你好!使用代码<h1></h1>

1 个答案:

答案 0 :(得分:1)

如果你设置了正文的textContent属性,它就知道它是文本而不是HTML(所以它会为你编码字符):

document.body.textContent = '<h1> Hello! </h1>`

否则:

document.body.innerHTML = '&gt;h1&lt; Hello! &gt;/h1&lt;';
相关问题