关于document.write()

关于document.write()

1. document.write()页面覆盖问题

如果在文档已完成加载后执行 document.write,整个 HTML 页面将被覆盖。

<body>
    <h1>
        hello
    </h1>
    <button onclick="document.write('hello123')">点击</button>
</body>

页面上的 <h1>hello</h1> 会被覆盖

<body>
    <h1>
        hello
    </h1>
    <script>document.write('hello123')</script>
</body>

'<h1>hello</h1>'不会被覆盖

2. 输出时候换行的报错

可以在文本字符串中使用反斜杠对代码行进行换行

document.write('你好    
世界')   //会报错
document.write('你好\  
世界')   //不会报错
posted @ 2021-09-12 22:44  CCCC_03  阅读(41)  评论(0)    收藏  举报