我在移动 View 中的选项卡中使用以下 html 代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
$(document).ready(function(){
$('a.tab-menu').click(function(){
if ( $(window).width() < 768 )
$('#tab-'+($('.tab-menu').index($(this))+1)).slideToggle("slow").siblings('div').hide('slow');
});
});
</script>
</head>
<body>
<h2 class="responsive-tab"><a href="#tab-1" class="tab-menu">tab 1</a></h2>
<div id="tab-1"> content here </div>
<h2 class="responsive-tab"><a href="#tab-2" class="tab-menu">tab 1</a></h2>
<div id="tab-2"> content here </div>
<h2 class="responsive-tab"><a href="#tab-3" class="tab-menu">tab 1</a></h2>
<div id="tab-3"> <a href="#">link here</a> </div>
</body>
</html>
我的问题是,如果单击所选选项卡上的任何链接并转到链接,并且当我单击后退按钮时应返回到所选选项卡,如何保留所选选项卡
最佳答案
您可以在页面准备好时查看location.hash
,然后在加载页面时触发对该选项卡的点击。
$(function () {
// Let's see if there's a hash in the url...
var hash = location.hash;
// If there is...
if (hash) {
// Find the link that has the same href value
// as the hash and fake a click on it...
$('a').filter(function () {
return this.href === hash;
}).trigger('click');
}
});
关于javascript - 单击浏览器后退按钮或移动设备后退按钮时保留所选选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33022501/