开发者

jquery - setting the url after the hash

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜