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

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

示例

假设外部文件的名称为demo.js。要将其添加到HTML页面中,请如下添加它-

<html>
   <head>
      <script src = "https://cdn.staticfile.org/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”)
});