Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何创建可以包含在其他HTML页面中的HTML文件?_Javascript_Html - Fatal编程技术网

Javascript 如何创建可以包含在其他HTML页面中的HTML文件?

Javascript 如何创建可以包含在其他HTML页面中的HTML文件?,javascript,html,Javascript,Html,如果我们想在多个页面上运行相同的html头代码,我们是否可以使用外部html文件或指向现有头代码的链接,而不是在每个页面中编写相同的代码 您可以使用jquery来完成。将此代码放在index.html中 <html> <head> <title></title> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script> $(funct

如果我们想在多个页面上运行相同的html头代码,我们是否可以使用外部html文件或指向现有头代码的链接,而不是在每个页面中编写相同的代码

您可以使用jquery来完成。将此代码放在index.html中

<html>
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script> 
$(function(){
  $("#header").load("header.html"); 
  $("#footer").load("footer.html"); 
});
</script> 
</head>
<body>
<div id="header"></div>
<!--Remaining section-->
<div id="footer"></div>
</body>
</html>
<a href="http://www.google.com">click here for google</a>

$(函数(){
$(“#header”).load(“header.html”);
$(“#footer”).load(“footer.html”);
});
并将此代码放在header.html和footer.html中与index.html相同的位置

<html>
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script> 
$(function(){
  $("#header").load("header.html"); 
  $("#footer").load("footer.html"); 
});
</script> 
</head>
<body>
<div id="header"></div>
<!--Remaining section-->
<div id="footer"></div>
</body>
</html>
<a href="http://www.google.com">click here for google</a>


然后访问index.html,您应该能够单击链接标签。

您可以使用jquery来完成。将此代码放在index.html中

<html>
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script> 
$(function(){
  $("#header").load("header.html"); 
  $("#footer").load("footer.html"); 
});
</script> 
</head>
<body>
<div id="header"></div>
<!--Remaining section-->
<div id="footer"></div>
</body>
</html>
<a href="http://www.google.com">click here for google</a>

$(函数(){
$(“#header”).load(“header.html”);
$(“#footer”).load(“footer.html”);
});
并将此代码放在header.html和footer.html中与index.html相同的位置

<html>
<head>
<title></title>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script> 
$(function(){
  $("#header").load("header.html"); 
  $("#footer").load("footer.html"); 
});
</script> 
</head>
<body>
<div id="header"></div>
<!--Remaining section-->
<div id="footer"></div>
</body>
</html>
<a href="http://www.google.com">click here for google</a>


然后访问index.html,您应该能够单击链接标记。

您可以使用IFRAME加载当前html页面中任何其他html页面的内容

<html>
    <head>
        YOUR CSS GOES HERE
    </head>
    <body>
        ANY TEXT YOU WANT
        <iframe src="PATH TO OTHER HTML PAGE">
        </iframe>
    </body>
</html>

你的CSS在这里
你想要什么文本

您可以使用IFRAME在当前HTML页面中加载任何其他HTML页面的内容

<html>
    <head>
        YOUR CSS GOES HERE
    </head>
    <body>
        ANY TEXT YOU WANT
        <iframe src="PATH TO OTHER HTML PAGE">
        </iframe>
    </body>
</html>

你的CSS在这里
你想要什么文本

关于它的一些文档。关于它的一些文档。如果您使用的是后端语言,最好是在服务器端而不是客户端进行。如果您使用的是PHP,那么您可以使用我想说的是,通过JS来实现这一点对于常见的部分来说通常是一个非常糟糕的主意。为此,您可能应该使用服务器端框架或生成工具。如果您使用的是后端语言,则最好使用服务器端而不是客户端。如果您使用的是PHP,那么您可以使用我想说的是,通过JS来实现这一点对于常见的部分来说通常是一个非常糟糕的主意。您可能应该使用服务器端框架或生成工具来实现这一点。但是,这并不是包含另一个文件的一种非常优雅的方式。应该在服务器端通过类似于
include()
的方式完成,或者如果需要,在客户端通过类似于@shas的方式完成。但是,这并不是包含另一个文件的非常优雅的方式。应该在服务器端通过类似于
include()
的方式完成,或者如果需要,可以在客户端通过类似于@shas的方式完成。