开发者

jQuery is canceling the default event operation for a hyperlink

开发者 https://www.devze.com 2023-02-14 22:15 出处:网络
I have assigned a .click() handler to an anchor element with jQuery to create an animating effect. The link changes the hash, which is then handled by an AJ开发者_如何学运维AX system. The problem is,

I have assigned a .click() handler to an anchor element with jQuery to create an animating effect. The link changes the hash, which is then handled by an AJ开发者_如何学运维AX system. The problem is, when I assign the .click() event function, the hash no longer changes automatically. I can manually change it with document.location.hash = "hash", but Firefox doesn't seem to be a fan of that. It works but behaves oddly when I load the page from my history.

Has anyone else encountered a problem like this?

<a href="#test">Test</a>

$('a[href="#test"]').click(function(){
 $(this).doSomething();
});


instead of your actual code try something like this

<a href="#test" class="sample">Test</a>

$(".sample").click(function()
{
  alert("href clicked"); // do your animation here
  return false;
}


Sorry to answer my own question again. It turns out I was only confusing myself. It is true that an ancestor element was assigned the click() event rather than the anchor itself. When I changed the event to reflect the anchor instead, I got nothing. Then I realized that the anchor was actually 1x1 dimensions. After re-CSS'ing it, the anchor is now the appropriate size of the list element and everything works properly.

Thank you all for your help!

0

精彩评论

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

关注公众号