How to write external js to call jquery

王林
Release: 2023-05-23 21:06:08
Original
621 people have browsed it

Before using an external js file to call the jQuery library, you need to confirm whether the jQuery library has been referenced in the page. If it is not referenced, you need to reference the jQuery library first in the html file.

The following are the steps for general external js files to call the jQuery library:

  1. Create a .js file and store it on the server or local host, such as sample.js.
  2. In sample.js, you first need to use jQuery's "document ready" function to ensure that the DOM tree has been loaded. The code is as follows:
$(document).ready(function(){
    // js代码内容
});
Copy after login
  1. Inside this function, write the js code you want to execute, such as replacing all text strings in the page, the code is as follows:
$(document).ready(function(){
    $('body').html($('body').html().replace(/关键词/g, '替换词'));
});
Copy after login
  1. Finally, introduce the sample.js file into the html file. The code is as follows:
<script src="sample.js" type="text/javascript"></script>
Copy after login

In this way, after the page is loaded, the relevant code in the sample.js file will be automatically executed. .

It should be noted that if you need to use other plug-ins or methods of jQuery in the sample.js file, you need to introduce other plug-ins or methods after introducing the jQuery library, otherwise you may be unable to call the jQuery function. Condition.

The above is the detailed content of How to write external js to call jquery. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!