是否應該始終將 JavaScript 檔案放在 HTML 檔案的 head 標記中?


你可以在網頁上的任何位置放置包含 JavaScript 的 <script> 標記,但通常建議你將其保留在 <head> 標記或 <body> 標記中。

在 <body> 元素中新增 JavaScript 對效能有好處。以下是在 <body>…</body> 中新增 <script> 的示例:-

<html>
   <body>
      <script>
         <!--
            document.write("Hello World!")
         //-->
      </script>
   </body>
</html>


以下是在 <head>…</head> 中新增 <script> 的示例:-

<html>
   <head>
      <script>
         <!--
            document.write("Hello World!")
         //-->
       </script>
   </head>
   <body>

   </body>
</html>

更新於:2020 年 6 月 13 日

362 次瀏覽

開啟你的 職業

完成課程獲得認證

開始使用
廣告
© . All rights reserved.