【发布时间】:2014-09-29 03:35:40
【问题描述】:
我自己成功创建了它....但问题是当我刷新页面时,所有内容都被隐藏了。我希望在第一次加载页面时看到第一个内容.....然后,如果用户单击第二个链接并刷新页面,他应该看到他在刷新之前单击的链接的内容。我认为将局部变量设为全局可以是答案……但我不知道如何制作。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#content").find("[id^='tab']").hide();
$("#tabs a").click(function(){
var dip=$(this).attr('name'); //to grasp the name attribute we used attr separately otherwise we could write $(this[name=''])
window.dip=dip;
$("#content").find("[id^='tab']").hide();
$("#content").find('#' + dip).fadeIn(); //if there are many similar elements in a parent element, find is used to search the required element with the help of its id.
});
});
</script>
</head>
<body>
<div id="uppertabs">
<ul id="tabs">
<li><a href="#" name="tab1">Profile</a></li>
<li><a href="#" name="tab2">Health</a></li>
</ul>
<div id="content">
<div id="tab1">Your profile is good</div>
<div id="tab2">Check your health.</div>
</div>
</div>
</body>
</html>
【问题讨论】:
-
为什么要刷新页面?除非我误解,否则您描述的问题是“预期行为”。每当刷新页面时,它都会返回到原始状态。所以给你的问题是:为什么要刷新页面?也许我们可以帮助解决 那个 问题(我怀疑 AJAX 将是你的救星......)
-
我在标签下使用了聊天代码...所以要获取消息,我需要刷新它。在一个选项卡中,我使用了动态内容搜索按钮,因此每当我单击它时,它都会返回到第一个选项卡...我必须再次打开该选项卡以查看结果。有什么解决办法吗?