开发者

jquery - setting the url after the hash

开发者 https://www.devze.com 2023-01-19 14:35 出处:网络
I\'d like if I could click a link with class \"query\" and have it\'s id attribute come after the hash. For example, a link that looks like this:

I'd like if I could click a link with class "query" and have it's id attribute come after the hash. For example, a link that looks like this:

<a href="#" id="members" class="query">Members</a>

When clicked would change the url from example.com/users to example.com/users#members.

Here's my code so far:

$('.query').click(function(event){
    event.preventDefault();
    window.location开发者_如何学Python.href = $(this).attr('id');
});

Right now clicking the link just moves the url to example.com/members


Set the hash property:

window.location.hash = $(this).attr('id');


Isn't it silly to have the id attribute come from the hash? Why not just ... Is it unreasonable to do this? Anyway, you can do this:

$(".query").click(function () {
   window.location.hash = this.id;
   return false; // prevent default link follow
});


Try window.location.hash instead of window.location.href

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号