Home  >  Article  >  Web Front-end  >  How to load external HTML files using jQuery's load method

How to load external HTML files using jQuery's load method

不言
不言Original
2018-12-12 17:03:248790browse

How to load external HTML files using How to load external HTML files using jQuerys load methods load method is a JavaScript library. The load method in How to load external HTML files using How to load external HTML files using jQuerys load methods load method can be used to load external HTML files. In this article, we will take a look at how the load method in How to load external HTML files using How to load external HTML files using jQuerys load methods load method loads external HTML files.

How to load external HTML files using How to load external HTML files using jQuerys load methods load method

First let’s take a look at what load is?

load can be used when reading content written on another HTML.

For example, when some menu bars are shared, it will be troublesome to write a menu bar on each page. In this case, you can use How to load external HTML files using How to load external HTML files using jQuerys load methods load method's load method to read the menus on other pages. column HTML file, so that the implemented code will not be redundant.

Then let’s seeHow to use the load method to load external HTML files?

Assuming you have a header written in the header.html file, if you read "sample.html" as an external file, you need the following script.

$(function(){
  $("#cover").load("header.html");
});

In addition, the above html is loaded to the "id" position set in "sample.html". Please note that "header.html" and "sample.html" are currently different files.

In "sample.html", How to load external HTML files using How to load external HTML files using jQuerys load methods load method may not be valid even if "header.html" is specified.

Therefore, the description about "Hader.html" is not "sample.html", but directly in "Hample.html".

Let’s look at a specific example

The code is as follows

HTML

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <script type="text/javascript" src="jquery-3.3.1.min.js">
      
    </script>
    <script src="sample.js" type="text/javascript"></script>
    <title>How to load external HTML files using How to load external HTML files using jQuerys load methods load method</title>
  </head>
  <body>
    <div id="cover">
      <div id="section1"></div>
      <div id="section2"></div>
    </div>
  </body>
</html>

HTML

<h1>header.html的标题</h1>

JavaScript

$(function(){
  $("#cover").load("header.html");
});

The operation effect is as follows

The content of "header.html" can be reflected in "sample.html".

How to load external HTML files using How to load external HTML files using jQuerys load methods load method

The above is the detailed content of How to load external HTML files using jQuery's load method. 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