jquery .click覆盖锚点href当我不想它! [英] jquery .click overriding anchor href when i dont want it to!

查看:318
本文介绍了jquery .click覆盖锚点href当我不想它!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组嵌套的DIV,当用户点击它们时,使用jQuery滑动。
在最内层DIV中有一个锚标签,其中包含一个应该在某处导航的HREF。
问题是,当我点击链接,它会像父母的DIVs,而不是导航到URL的切换。
如果我右键单击锚点并选择在新选项卡中打开,那么导航很好。
请你能发现什么错了吗?
感谢

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>&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; 
}); 


推荐答案

这里有一些修改,相同的选择器,并检查事件不是来自< a> 标签,如下所示:

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; 
});

您可以在这里测试。如果事件目标来自< a> (没有孩子),那么我们只是退出处理程序,返回 undefined ,因为我们没有显式返回 false 点击事件将做正常的事情...转到 href

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.

这篇关于jquery .click覆盖锚点href当我不想它!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆