你能链接到一个HTML文件吗? [英] Can you link to an HTML file?

查看:131
本文介绍了你能链接到一个HTML文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站始终有相同的导航菜单,而不是为每个页面重写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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆