是否可以在不导致页面跳转到顶部的情况下从window.location中删除哈希?我需要能够在不引起任何 skip 的情况下修改哈希.

I have this:

$('<a href="#123">').text('link').click(function(e) {
  e.preventDefault();
  window.location.hash = this.hash;
}).appendTo('body');

$('<a href="#">').text('unlink').click(function(e) {
  e.preventDefault();
  window.location.hash = '';
}).appendTo('body');

See live example here: http://jsbin.com/asobi

当用户点击‘link’时,哈希标签被修改,没有任何页面跳转,所以工作正常.

But when the user clicks 'unlink' the has tag is removed and the page scroll-jumps to the top. I need to remove the hash without this side-effect.

推荐答案

我相信,如果你只是在一个虚拟哈希,它不会滚动,因为没有匹配滚动.

<a href="#A4J2S9F7">No jumping</a>

<a href="#_">No jumping</a>

"#" by itself is equivalent to "_top" thus causes a scroll to the top of the page

Jquery相关问答推荐

在shiny 的datatable列中启用智能搜索

JQuery UI-AutoComplete未显示下拉菜单

如何在光滑的轮播上添加进度条

ASP.NET MVC - Ajax 将空值传递给控制器

jquery如何判断ajax调用的响应类型

如何在jQuery中移动表格行?

通过 .push() 方法向对象添加项目

使用 jQuery click 处理锚点 onClick()

如何正确卸载/销毁 VIDEO 元素

如何通过 Select 插件使用 jQuery 验证?

页面滚动后,jQuery可拖动在错误的位置显示助手

JavaScript style.display="none" 还是 jQuery .hide() 更高效?

如何使用 jQuery Select 的插件重置表单?

验证外部脚本是否已加载

将图像 url 转换为 Base64

jQuery vs jQuery Mobile vs jQuery UI?

通过 :not 在 jQuery Select 器中隐藏除 $(this) 之外的所有内容

使用 Jquery 查找父 div 的 id

Fancybox 不适用于 jQuery v1.9.0 [ f.browser 未定义 / 无法读取属性 'msie' ]

使用 jQuery 检测元素内容的变化