我正在使用jQuery在onload用户点击帖子标题时在特定主题标签(通过主体类中的函数)加载页面.标签显然出现在网址中.我想知道是否有办法隐藏主题标签使URL混乱.有些搜索并没有提出太多.
function goToAnchor() {
location.href = "#post";
}
Run Code Online (Sandbox Code Playgroud)
Jef*_*nes 22
如下所示:http://djpate.com/2009/10/07/animated-scroll-to-anchorid-function-with-jquery/
function goToByScroll(id){
$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}
Run Code Online (Sandbox Code Playgroud)
只需传递要滚动的元素的id即可.
您可以将click事件绑定到此特定类型的锚点,并防止其上的默认值:
$('.anchorClass').on('click', function(e) {
e.preventDefault();
// rest of stuff
});
Run Code Online (Sandbox Code Playgroud)
"剩下的东西"意味着找到一些会跳转页面的插件或代码示例.如果你想让它顺利滚动,那么有一个很受欢迎的scrollTo插件.甚至可能是scrollTo插件负责默认预防.
[更新]
杰夫的建议(假设它有效)是"剩下的东西",这两个答案中更有用.;-)但是防止违约仍然很重要.