Home  >  Article  >  Web Front-end  >  How to write external js to call jquery

How to write external js to call jquery

王林
王林Original
2023-05-23 21:06:08677browse

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代码内容
});
  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, '替换词'));
});
  1. Finally, introduce the sample.js file into the html file. The code is as follows:
<script src="sample.js" type="text/javascript"></script>

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!

Statement:
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