我正在尝试创建一个分为两个交互式框架的网站。如果单击左侧的链接,则左侧将重新加载新内容,而右侧将保持不变(并且不会刷新)。
如果我只单击一次侧栏顶部的链接,它的行为就很好。单击两次相同的链接后,导航元素消失了。
这是我的侧边栏的内容代码:
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$.get("sidebar-navigation.html", function(data){
$(".sidebar-nav").html(data);
});
/*
Use $.on() to associate a click handler with any link nested under .sidebar-navigation,
even if the links are dynamically added to the page in the future (ie after a
$.get())
*/
$('body').on('click', '.sidebar-nav a', function() {
/*
Get the href for the link being clicked
*/
var href = $(this).attr('href');
/*
Issue a request for the html document
or resource at href and load the contents
directly into .sidebar if successful
*/
$('.sidebar-content').load(href);
/*
Return false to prevent the link's default
navigation behavior
*/
return false;
})
</script>
</head>
<body>
<div class="sidebar-content">
<div class="sidebar-nav">
</div>
<p>
My sidebar content
</p>
</div>
</body>
</html>
答案 0 :(得分:0)
您的答案是正确的,您只需要将目标定位得更好
<html>
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$.get("sidebar-navigation.html", function(data){
$(".sidebar-nav").html(data);
});
/*
Use $.on() to associate a click handler with any link nested under .sidebar-navigation,
even if the links are dynamically added to the page in the future (ie after a
$.get())
*/
$('body').on('click', '.sidebar-nav a', function() {
/*
Get the href for the link being clicked
*/
var href = $(this).attr('href');
/*
Issue a request for the html document
or resource at href and load the contents
directly into .sidebar if successful
*/
$('#stick-my-stuff-here').load(href);
/*
Return false to prevent the link's default
navigation behavior
*/
return false;
})
</script>
</head>
<body>
<div class="sidebar-content">
<div class="sidebar-nav">
</div>
<p id="stick-my-stuff-here">
My sidebar content
</p>
</div>
</body>
</html>
答案 1 :(得分:0)
我认为最终发生的事情是我呼吁在不应该将其添加到目标页面时重新加载侧边栏导航。当我从要显示在侧边栏中的其他页面上删除了sidebar-nav
div后,一切正常。