你能链接到一个HTML文件吗? [英] Can you link to an HTML file?
问题描述
我的网站始终有相同的导航菜单,而不是为每个页面重写HTML代码,我可以链接到第二个HTML文件(包含导航HTML代码),就像使用CSS一样?或者会产生问题?
简单的方法是将标题部分放在单独的html文件中。
现在使用jQuery 加载
函数(如
$(#headerDiv)。load(header.html)
知道这将需要Web服务器,因为加载函数会向服务器发送请求。
查看代码示例:
demo.html
< html>
< head>
< script src =https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js>< / script>
< script>
$(function(){
$(#headerDiv)。load(header.html);
});
< / script>
< / head>
< body>
< div id =headerDiv>< / div>
<! - 其余代码 - >
< / body>
< / html>
header.html
< div>
< a>某事< / a>
< a>某事< / a>
< / div>
My website has the same navigation menu throughout, instead to rewriting the HTML code for every page, can I link to a second HTML file (that contains the nav HTML code) like you would with CSS? Or will that create problems?
Simple way would be to put the header part in a separate html file.
Now load this file in html code using jQuery load
function like
$("#headerDiv").load("header.html")
Know that, this will require web server because load function sends a request to server.
Check out the code sample:
demo.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
$(function(){
$("#headerDiv").load("header.html");
});
</script>
</head>
<body>
<div id="headerDiv"></div>
<!-- Rest of the code -->
</body>
</html>
header.html
<div >
<a>something</a>
<a>something</a>
</div>
这篇关于你能链接到一个HTML文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!