JQuery - url location and click event

扶醉桌前 提交于 2019-12-24 07:46:43

问题


$('#head_menu a').click(function(){ currentPage = document.location.hash.split('#')[1]; });

<div id="head_menu">
    <a href="#order"><div>make order</div></a>
    <a href="#portfolio"><div>portfolie</div></a>
    <a href="#contacts"><div>contacts</div></a>
    <a href="#vacancies"><div>vacancies</div></a>
    <a href="#about"><div>about company</div></a>
</div>

So, when I'm clicking to the links in head_menu div I come into this handler. But one trouble: if url has, for example, #about hash, in handler I'll get 'about', not that, at what I've clicked. Could I get new url-hash in click-handler?


回答1:


Why not just look at the href attribute of the link that was clicked?

$('#head_menu a').click(function(){
   currentPage = this.href.split('#')[1];
});



回答2:


Don't understand question ;)

BTW:

Another person who insert div into a. "a" is inline element so it couldn't has Div (it is block elemennt)



来源:https://stackoverflow.com/questions/1511368/jquery-url-location-and-click-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!