我使用这个脚本:
(function() {
var tabContainers = $('div.tabs > div');
tabContainers.hide().filter(':first').show();
$(window).bind('hashchange', function () {
var hash = window.location.hash || '#first';
tabContainers.hide();
tabContainers.filter(hash).show();
$('div.tabs ul.tabNavigation a').removeClass('selected');
$('a[href=' + hash +']').addClass('selected');
});
$(window).trigger("hashchange");
});
这部分代码为我提供了我需要的一切。例如,要从外部页面打开选项卡,我使用“index.php#first”href。它打开带有选项卡的页面,并打开了确切的选项卡,但它跳转到 anchor ,我需要从顶部显示页面。
有人知道如何防止此代码的 anchor 跳转吗?
最佳答案
您是否尝试过在 hashchange 函数中执行 return false
操作?
否则,您可以避免在元素上使用真实 ID。这样它就不会滚动。
更新:
当您使用 jQuery 时,您还可以尝试使用:e.preventDefault
。该函数需要接收 e
作为参数
关于jQuery Tabs,如何防止 anchor 跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12914034/