Javascript 如何在另一个html文件中包含html文件
我有两个html文件Javascript 如何在另一个html文件中包含html文件,javascript,jquery,html,Javascript,Jquery,Html,我有两个html文件first.html和second.html,我想将第二个文件包含到第一个文件中,以便代码重用。我使用以下语句来包含该文件: <link rel="import" href="html file name here"> 但它不起作用。我也尝试使用其他解决方案,但没有一个有效 所以请给出任何建议。使用jquery的.load()函数加载模板。 请参阅此文档 使用jQuery: a.html: <html> <head> <
first.html
和second.html
,我想将第二个文件包含到第一个文件中,以便代码重用。我使用以下语句来包含该文件:
<link rel="import" href="html file name here">
但它不起作用。我也尝试使用其他解决方案,但没有一个有效
所以请给出任何建议。使用jquery的.load()
函数加载模板。
请参阅此文档 使用jQuery:
a.html:
<html>
<head>
<script src="jquery.js"></script>
<script>
$(function(){
$("#content").load("b.html");
});
</script>
</head>
<body>
<div id="content"></div>
</body>
</html>
b.html:
<p> This is my second file </p>
a.html:
$(函数(){
$(“#content”).load(“b.html”);
});
b、 html:
这是我的第二份文件
看看这个网站。
它提出了哪些最佳实践。
这取决于你的实际需要。为什么不使用框架?在我看来,最好的解决方案是: 使用jQuery:
a.html:
<html>
<head>
<script src="jquery.js"></script>
<script>
$(function(){
$("#content").load("b.html");
});
</script>
</head>
<body>
<div id="content"></div>
</body>
</html>
b.html:
<p> This is my second file </p>
a、 html:
<html>
<head>
<script src="jquery.js"></script>
<script>
$(function(){
$("#includedContent").load("b.html");
});
</script>
</head>
<body>
<div id="includedContent"></div>
</body>
</html>
b.html:
<p> This is my include file </p>
$(函数(){
$(“#includedContent”).load(“b.html”);
});
b、 html:
这是我的包含文件
这样我就可以得到一个简单明了的解决方案
资料来源:
谷歌是你的朋友:)可能重复