如何将 jQuery 代码放入外部 .js 文件中?

jqueryweb developmentfront end technology更新于 2026/2/17 0:37:17

创建一个外部 JavaScript 文件并在其中添加 jQuery 代码。

示例

假设外部文件的名称为 demo.js。要将其添加到 HTML 页面中,请像下面这样引用它 −

<html>
   <head>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script src="demo.js"></script>
   </head>

   <body>
      <h1>Hello</h1>
   </body>
</html>

上面我们使用 Google CDN 添加了 jQuery,之后引入了外部文件。
由于您想将 jQuery 代码放置在 demo.js 中,因此请在其中添加 jQuery 代码 −

$(document).ready(function(){
   alert(“amit”)
});



相关文章