gpt4 book ai didi

javascript - 页面之间的导航 - html

转载 作者:行者123 更新时间:2023-12-03 05:19:19 24 4
gpt4 key购买 nike

我尝试在包含相同页眉和页脚的 3 个页面之间导航,但每个页面都有不同的内容。我想在哈希更改时加载不同内容的 html。问题是,当我再次单击同一页面时,content.html 会再次加载。

如何使用 java script/html/jquery 来使用内容而不需要一次又一次加载 html?

代码示例:

导航栏.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Navigation Bar</title>
<link rel="stylesheet" type="text/css" href="css/navigationbar.css">

</head>
<body>
<nav>
<img id="navigation-bar-logo" class="logo" src='images/flybryceLogo.png'>
<ul class="navigation-bar-ul">
<li class="navigation-bar-li"><a id="navigation-bar-contact-page-tab" href="#contact.html">CONTACT</a></li>
<li class="navigation-bar-li"><a id="navigation-bar-about-us-page-tab" href="#aboutus.html">ABOUT US</a></li>
<li class="navigation-bar-li"><a id="navigation-bar-home-page-tab" href="#home.html">HOME</a></li>
</ul>
</nav>
</body>
</html>

初始.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>One Page Test</title>
<link rel="stylesheet" type="text/css" href="css/homepage.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
<div id="main-container" class="main-container">
<div id="header" class="header">
</div>
<div id="content" class="content"></div>
<div id="footer" class="bot"></div>
</div>

<script>
document.onreadystatechange = function(){
if (document.readyState == 'complete') {
window.onhashchange=hash_change;
window.onload=hash_change;
if(window.location.hash==''){
//default hash
window.location.replace('#home.html');
}

//load the header
$("#header").load("fragments/navigationbar.html");
//load the footer
$("#footer").load("fragments/footer.html");
}
}

function hash_change()
{
//get the new hash
var newHashCode = window.location.hash.substring(1);

if (newHashCode === "home.html"){
$("#content").load("home.html");

} else if (newHashCode === "aboutus.html") {
$("#content").load("aboutus.html");

} else if (newHashCode === "contact.html"){
$("#content").load("contact.html");

}

}
</script>
</body>
</html>

最佳答案

一个更长但合适的解决方案是自己构建内容缓存。

例如,仅向服务器请求一次 html,然后将其设置为 $('#content') 元素。您可以使用这个辅助函数。

var contentsCache = {};
var getAndCache = function(url, callback) {
var cachedContents = contentsCache[url];

if (!cachedContents) {
$.get(url, function(serverContents) {
cachedContents = serverContents;
contentsCache[url] = cachedContents;
callback(cachedContents);
});
} else {
callback(cachedContents);
}
};

然后通过调用这种新的异步方式来替换 $('#content').load 调用。

function hash_change()
{
var fillContentCb = function(s) {
$('#content').html(s);
};
//get the new hash
var newHashCode = window.location.hash.substring(1);

if (newHashCode === "home.html"){
getAndCache("home.html", fillContentCb);

} else if (newHashCode === "aboutus.html") {
getAndCache("aboutus.html", fillContentCb);
} else if (newHashCode === "contact.html"){
getAndCache("content.html", fillContentCb);
}
}

正如一些评论中所建议的,请考虑使用 native HTML 导航。另一个建议是,如果该应用程序可能会增长,则使用支持路由的客户端 JS 框架。

关于javascript - 页面之间的导航 - html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41463715/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com