Đôi khi, chúng ta phải tải một tệp HTML bên ngoài vào một tệp HTML khác bằng JavaScript hoặc jQuery, tùy thuộc vào yêu cầu của dự án. Hướng dẫn này minh họa cách tải tệp HTML bên ngoài bằng JavaScript và jQuery
Sử dụng phương pháp JavaScript
Click to load content from home.html file
The content from Home Page will be displayed here
0 để tải tệp HTML bên ngoài
Click to load content from home.html file
The content from Home Page will be displayed here
Mã HTML [______31]
Home Page
This content is loaded from the home page
Mã HTML [______10]
Click to load content from home.html file
The content from Home Page will be displayed here
Mã CSS [______11]
div{
border: 2px solid rgb[255, 0, 0];
width: fit-content;
margin: 5px;
padding: 2px 20px;
cursor: pointer;
}
button{
margin: 5px;
padding: 2px 20px;
}
p{
font-size: 14px;
}
Mã JavaScript [
Click to load content from home.html file
The content from Home Page will be displayed here
2]function loadHTML[]{
fetch['home.html']
.then[response=> response.text[]]
.then[text=> document.getElementById['homePage'].innerHTML = text];
}
đầu ra
Hàm
Click to load content from home.html file
The content from Home Page will be displayed here
0 yêu cầu máy chủ tải dữ liệu trên các trang web khác nhauChúng tôi sử dụng hàm
Click to load content from home.html file
The content from Home Page will be displayed here
0 để tìm nạp tệp
Click to load content from home.html file
The content from Home Page will be displayed here
1 từ localhost. Phương thức
Click to load content from home.html file
The content from Home Page will be displayed here
0 trả về một
Click to load content from home.html file
The content from Home Page will be displayed here
7Hơn nữa, phương thức
Click to load content from home.html file
The content from Home Page will be displayed here
9 của giao diện
Click to load content from home.html file
The content from Home Page will be displayed here
8 được sử dụng, phương thức này chấp nhận luồng
Click to load content from home.html file
The content from Home Page will be displayed here
8, đọc nó và trả về một
Click to load content from home.html file
The content from Home Page will be displayed here
7, phương thức này giải quyết bằng phương pháp div{
border: 2px solid rgb[255, 0, 0];
width: fit-content;
margin: 5px;
padding: 2px 20px;
cursor: pointer;
}
button{
margin: 5px;
padding: 2px 20px;
}
p{
font-size: 14px;
}
2. Hãy nhớ rằng,
Click to load content from home.html file
The content from Home Page will be displayed here
8 được giải mã bằng cách sử dụng UTF-8Sau đó, chúng tôi nhận được phần tử có id là
div{
border: 2px solid rgb[255, 0, 0];
width: fit-content;
margin: 5px;
padding: 2px 20px;
cursor: pointer;
}
button{
margin: 5px;
padding: 2px 20px;
}
p{
font-size: 14px;
}
4 bằng cách sử dụng phương pháp div{
border: 2px solid rgb[255, 0, 0];
width: fit-content;
margin: 5px;
padding: 2px 20px;
cursor: pointer;
}
button{
margin: 5px;
padding: 2px 20px;
}
p{
font-size: 14px;
}
5 và thay thế HTML bên trong của nó thông qua thuộc tính div{
border: 2px solid rgb[255, 0, 0];
width: fit-content;
margin: 5px;
padding: 2px 20px;
cursor: pointer;
}
button{
margin: 5px;
padding: 2px 20px;
}
p{
font-size: 14px;
}
6 bằng div{
border: 2px solid rgb[255, 0, 0];
width: fit-content;
margin: 5px;
padding: 2px 20px;
cursor: pointer;
}
button{
margin: 5px;
padding: 2px 20px;
}
p{
font-size: 14px;
}
7Sử dụng phương pháp jQuery div{
border: 2px solid rgb[255, 0, 0];
width: fit-content;
margin: 5px;
padding: 2px 20px;
cursor: pointer;
}
button{
margin: 5px;
padding: 2px 20px;
}
p{
font-size: 14px;
}
8 để tải tệp HTML bên ngoài
div{
border: 2px solid rgb[255, 0, 0];
width: fit-content;
margin: 5px;
padding: 2px 20px;
cursor: pointer;
}
button{
margin: 5px;
padding: 2px 20px;
}
p{
font-size: 14px;
}
Mã HTML [______31]
Home Page
This content is loaded from the home page
Mã HTML [______10]
Click to load content from home.html file
The content from Home Page will be displayed here
1Mã CSS [______11]
Click to load content from home.html file
The content from Home Page will be displayed here
3Mã JavaScript [
Click to load content from home.html file
The content from Home Page will be displayed here
2]
Click to load content from home.html file
The content from Home Page will be displayed here
5đầu ra
Phương pháp
function loadHTML[]{
fetch['home.html']
.then[response=> response.text[]]
.then[text=> document.getElementById['homePage'].innerHTML = text];
}
3 kiểm tra xem tệp đã hoàn toàn sẵn sàng hay chưa. Sự kiện này chỉ xảy ra khi Mô hình đối tượng tài liệu đã được tải đầy đủPhương thức
div{
border: 2px solid rgb[255, 0, 0];
width: fit-content;
margin: 5px;
padding: 2px 20px;
cursor: pointer;
}
button{
margin: 5px;
padding: 2px 20px;
}
p{
font-size: 14px;
}
8 tải thông tin [dữ liệu] từ máy chủ và lấy dữ liệu do máy chủ trả về vào phần tử được chỉ địnhChúng tôi sử dụng phương pháp
function loadHTML[]{
fetch['home.html']
.then[response=> response.text[]]
.then[text=> document.getElementById['homePage'].innerHTML = text];
}
3 để đảm bảo rằng DOM hoàn toàn sẵn sàng trước các hoạt động tiếp theo. Tệp
Click to load content from home.html file
The content from Home Page will be displayed here
1 sẽ được tải bằng phương pháp div{
border: 2px solid rgb[255, 0, 0];
width: fit-content;
margin: 5px;
padding: 2px 20px;
cursor: pointer;
}
button{
margin: 5px;
padding: 2px 20px;
}
p{
font-size: 14px;
}
8