include jQuery to html

2019-09-30 11:10发布

问题:

I was working with jQuery on https://codepen.io/ and to include jQuery is very easy. So i decided to work on files on my computer, i tried to include it using the file and it didn't work. also i tried to include from CDN but still doesn't work.

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

and in the Web.js i put this code:

<button id="btn">Hide</button>
<p id="para">A dpara.</p>

回答1:

Your Web.js isn't a javascript file, but rather an HTML-file. You want to create a new HTML file named for example index.html. You fill this one up like so:

<!--  index.html: -->
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="js/Web.js"></script>
  </head>
  <body>
    <button id="btn">Hide</button>
    <p id="para">A dpara.</p>
  </body>
</html>

You should be able to use jQuery as you wish now.

Also, this is the folder structure you should have with the above example:

  • index.html
  • js
    • Web.js