为什么这个Javascript在呈现的HTML中打印我的注释

Vin*_*eau 0 javascript comments

我是Javascript的新手,就像今天刚开始使用Javascript一样.我正在测试一本书第一章中的一些非常基本的代码,并在第一个例子中遇到了问题.我在Notepad ++中编写了代码,但即使在该程序中,我的第二行注释是黑色而不是绿色.为什么</noscript>渲染后的线条正确?

在Notepad ++中呈现的代码

我浏览器的输出呈现为: Hello World! // Display a message dialog after the page has loaded.

<!DOCTYPE html>
<html>
<body>
    <div id = "panel">
    <script type = "text/javascript">
    // Dynamically write a text string as the page loads.
    document.write("Hello World!");
    </script>
    <noscript>Javascript is Not Enabled!</noscript>
    // Display a message dialog after the page has loaded.
    <body onload = " window.alert('Document Loaded!');">
    </div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Den*_*ret 7

那是因为你的评论不是在JavaScript部分,而是在HTML部分.

HTML中的注释如下:

<noscript>Javascript is Not Enabled!</noscript> 
<!-- Display a message dialog after the page has loaded. -->
Run Code Online (Sandbox Code Playgroud)

请注意,你已经将一个body元素放入其中body,这并不好.你可能想要这个而不是第二个身体:

<script>
     window.onload = function(){
          alert('document loaded!');
     };
</script>
Run Code Online (Sandbox Code Playgroud)