jquery .click 覆盖锚点 href 当我不想要它时!
我有一组嵌套的 DIV,当用户单击它们时,它们使用 jQuery 进行滑动切换。 在最里面的 DIV 内有一个带有 HREF 的锚标记,可以导航到某个位置。 问题是,当我单击链接时,它会像父 DIV 一样滑动切换,而不是导航到 url。 如果我右键单击锚点并选择在新选项卡中打开,则可以正常导航。 请问你能发现出了什么问题吗? 谢谢
<div class="pod">
<li id='ThirdParty'>
<div class='block'>
<h1>ThirdParty</h1>
<div class='systemHeader'>
<h2><span>Bobs shop</span></h2>
<div class='subSystems'>
<div class='subSystemHeader'>
<h3><span> Gifts</span></h3>
<div class='reports '>
<p class='reports i1'>
<a href='/Next.Whs.Web.MenuSystem/Default.aspx?id=470' title=''>Option 1</a></p>
</div>
</div>
</div>
</div>
</div>
</li>
</div>
$("div.subSystemHeader, div.subSystemHeader").click(function() {
$("> div", this).slideToggle(...);
return false;
});
I have a set of nested DIVs that slidetoggle using jQuery as the user clicks on them.
Inside the innermost DIV there is an anchor tag with an HREF that should navigate somewhere.
The problem is that when I click on the link it slidetoggles just like the parent DIVs instead of navigating to the url.
If I right click the anchor and select open in new tab then that navigates fine.
Please can you spot whats going wrong?
Thanks
<div class="pod">
<li id='ThirdParty'>
<div class='block'>
<h1>ThirdParty</h1>
<div class='systemHeader'>
<h2><span>Bobs shop</span></h2>
<div class='subSystems'>
<div class='subSystemHeader'>
<h3><span> Gifts</span></h3>
<div class='reports '>
<p class='reports i1'>
<a href='/Next.Whs.Web.MenuSystem/Default.aspx?id=470' title=''>Option 1</a></p>
</div>
</div>
</div>
</div>
</div>
</li>
</div>
$("div.subSystemHeader, div.subSystemHeader").click(function() {
$("> div", this).slideToggle(...);
return false;
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里进行一些更改,无需重复相同的选择器,并检查事件是否来自
标记,如下所示:
您可以在这里测试。如果事件目标来自
(它没有子级),那么我们只需退出处理程序,返回
undefined
,因为我们没有显式返回false
click
事件会做正常的事情...转到href
。A few changes here, there's no need to repeat the same selector, and check that the event didn't come from an
<a>
tag, like this:You can test it here. If the event target was from the
<a>
(there are no children of it) then we just exit the handler, returningundefined
, since we're not explicitly returningfalse
theclick
event will do it's normal thing...going to thehref
.您可以检查单击的事件对象,然后检查事件目标 - 另请参阅 http://api. jquery.com/event.target/
(没有测试它,但它应该可以工作)
you can check the event object of the click and then check the event target - see also http://api.jquery.com/event.target/
(didnt test it, but it should work)
您的问题是
return false;
它阻止了默认行为。Your problem is
return false;
which is blocking the default behavior.我今天刚刚遇到这个问题。我发现一篇非常有趣的文章可能会有所帮助。它描述了“return false”和 Jquery 事件的误用(http: //fuelyourcoding.com/jquery-events-stop-misusing-return-false/)。我尚未在您的示例中测试以下代码,但我认为这应该有效。
I just came across this issue today. I found a very interesting article that might help. It describes the mis(use) of 'return false' and Jquery events (http://fuelyourcoding.com/jquery-events-stop-misusing-return-false/). I haven't tested the following code on your example, but I think this should work.