Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

W3.JS HTML 包含

包含 HTML 文件

w3.includeHTML()

HTML 代码

将您要包含的 HTML 保存到 .html 文件中

content.html

<a href="https://w3schools.org.cn/html/">HTML</a><br>
<a href="https://w3schools.org.cn/css/">CSS</a><br>
<a href="https://w3schools.org.cn/bootstrap/">Bootstrap</a><br>
<a href="https://w3schools.org.cn/js/">JavaScript</a><br>
<a href="https://w3schools.org.cn/sql/">SQL</a><br>
<a href="https://w3schools.org.cn/php/">PHP</a><br>
<a href="https://w3schools.org.cn/w3css/">W3.CSS</a><br>

包含 HTML

包含 HTML 是通过使用 w3-include-html 属性完成的

示例

<div w3-include-html="content.html"></div>

添加 JavaScript

HTML 包含由 JavaScript 完成。

确保您的页面加载了 w3.js 并调用 w3.includeHTML()

示例

<script>
w3.includeHTML();
</script>

完整示例

示例

<!DOCTYPE html>
<html>
<script src="/lib/w3.js"></script>
<body>

<div w3-include-html="content.html"></div>

<script>
w3.includeHTML();
</script>

</body>
</html>
尝试一下 » 带有 CSS »

包含多个 HTML 代码片段

您可以包含任意数量的 HTML 代码片段

示例

<!DOCTYPE html>
<html>
<script src="/lib/w3.js"></script>
<body>

<div w3-include-HTML="h1.html"></div>
<div w3-include-HTML="content.html"></div>

<script>
w3.includeHTML();
</script>

</body>
<html>

尝试一下 » 带有 CSS »


添加回调函数

当您在网页中包含 HTML 代码片段时,必须确保依赖于包含的 HTML 的其他函数在 HTML 正确包含之前不会执行。

“延迟”代码的最简单方法是将其放在回调函数中。

回调函数可以作为参数添加到 w3.includeHTML() 中

示例

<script>
w3.includeHTML(myCallback);

function myCallback() {
  // 需要等待的代码放在这里
}
</script>

您将在本教程的下一章中找到一个回调示例。


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.