开发者

jQuery steals click from children

开发者 https://www.devze.com 2023-01-21 14:44 出处:网络
Say i have <h3> some text <a href=\"google.com\">google</a> </h3> I want to attach a click event to the h3

Say i have

<h3>
  some text
  <a href="google.com">google</a>
</h3>

I want to attach a click event to the h3

$("h3").click(function(){ $(this).slideDown(); return false; });

but I also want to preserve the clicking on the actual link. Is there a way to do this with jQuer开发者_如何学Cy?

Thanks!


You can check the event's actual .target and do nothing if it was an <a> element, for example:

$("h3").click(function(e){ 
  if(e.target.nodeName == 'A') return;
  $(this).slideDown(); 
  return false; 
});


Just don't prevent the default behavior:

$("h3").click(function(){ $(this).slideDown(); });

Also, do you mean .slideUp()? How can you click on something before it slides down?

Try it out with this jsFiddle

0

精彩评论

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