location.hash立即消失 [英] location.hash disappears immediately

查看:96
本文介绍了location.hash立即消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个实时搜索+过滤方法,并且我注意到当我使用该方法(这是一个示例)时,它在URL中显示后进行哈希 - 它会立即被删除。如果我将该哈希集移动到例如按钮的单击方法它工作正常。为什么?

I'm building a live search + filter method, and I've noticed when I use that method (it's an example) that hash after it shows in the URL - it is immediately being removed. If I move that hash set to e.g. button's click method it works fine. Why ?

$('.menu-link').click(function () {            
        window.location.hash = 'search-container';
});


推荐答案

它正在消失,因为您的链接被跟踪,导致要更改为 a href 的位置。如果您将返回false; 添加到点击处理程序的末尾,则该位置不会更改。

It's disappearing because your link is being followed, causing the location to be changed to the href of the a. If you add a return false; to the end of your click handler, then the location won't change.

这篇关于location.hash立即消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆