document.write会取一个文本串,把它插入到文档中。实际上,它会在script标记所在的位置输出这个串。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript"> document.write('这里的js代码被写在head里面'); </script> </head> <body> <p>这里是标题,是为了测试document.write会在何处输出,JavaScript代码将会写在下面</p> <script type="text/javascript"> document.write('这里是JS代码'); </script> <p>这里是javascript代码结束的部分</p> </body> <script type="text/javascript"> document.write('holaholahola') </script> </html>
运行效果: