jquery .click 覆盖锚点 href 当我不想要它时!

发布于 2024-09-27 18:04:24 字数 1228 浏览 4 评论 0原文

我有一组嵌套的 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>&nbsp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小猫一只 2024-10-04 18:04:24

这里进行一些更改,无需重复相同的选择器,并检查事件是否来自 标记,如下所示:

$("div.subSystemHeader").click(function(e) { 
  if(e.target.nodeName == 'A') return; //skip this handler, don't return false
  $("> div", this).slideToggle(...); 
  return false; 
});

您可以在这里测试。如果事件目标来自 (它没有子级),那么我们只需退出处理程序,返回 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:

$("div.subSystemHeader").click(function(e) { 
  if(e.target.nodeName == 'A') return; //skip this handler, don't return false
  $("> div", this).slideToggle(...); 
  return false; 
});

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, returning undefined, since we're not explicitly returning false the click event will do it's normal thing...going to the href.

小帐篷 2024-10-04 18:04:24

您可以检查单击的事件对象,然后检查事件目标 - 另请参阅 http://api. jquery.com/event.target/

 $("div.subSystemHeader, div.subSystemHeader").click(function(event) { 
   if(event.target.nodeName.toLowerCase() == 'a') return;
 ...
 }

(没有测试它,但它应该可以工作)

you can check the event object of the click and then check the event target - see also http://api.jquery.com/event.target/

 $("div.subSystemHeader, div.subSystemHeader").click(function(event) { 
   if(event.target.nodeName.toLowerCase() == 'a') return;
 ...
 }

(didnt test it, but it should work)

情归归情 2024-10-04 18:04:24

您的问题是 return false; 它阻止了默认行为。

$("a:eq(0)").click(function() {
    return false; /* does nothing */
});
$("a:eq(1)").click(function() {
    /* go to href */
});

Your problem is return false; which is blocking the default behavior.

$("a:eq(0)").click(function() {
    return false; /* does nothing */
});
$("a:eq(1)").click(function() {
    /* go to href */
});
顾铮苏瑾 2024-10-04 18:04:24

我今天刚刚遇到这个问题。我发现一篇非常有趣的文章可能会有所帮助。它描述了“return false”和 Jquery 事件的误用(http: //fuelyourcoding.com/jquery-events-stop-misusing-return-false/)。我尚未在您的示例中测试以下代码,但我认为这应该有效。

$('.subSystemHeader a').click(function(e){
    e.stopPropagation(); 
});

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.

$('.subSystemHeader a').click(function(e){
    e.stopPropagation(); 
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文