简体   繁体   English

jquery切换阻止了孩子的点击事件

[英]jquery toggle blocks child's click event

Could somebody tell me how to achive the following <a> element not to be blocked after toogle? 有人可以告诉我如何在toogle之后实现以下<a>元素不被阻止? (It is visible, but I cant click on it) Thanks! (它是可见的,但我无法点击它)谢谢!

<div id='gear_icon'>
   <div id='gear_div'>
      <ul>
         <li>
            <a href="go.php">I'M BLOCKED</a></li></ul></div></div>

$("#gear_icon").toggle(function(e)
{
    if(e.target === this )
    //show gear-div
    $('#gear_div').css({display:"block",opacity: 0.0}).animate({opacity: 1}, 1000);
},function(e)
{
    if(e.target === this )
    //hide gear_div
    $('#gear_div').animate({opacity:0},1000,function()
    {
        $('#gear_div').css({display:"none"});
    }); 
});

The problem is that the child also calls the toggle event. 问题是孩子也调用了切换事件。 if(e.target === this ) line makes it ignored. if(e.target === this )其被忽略。

Here is the css code: 这是css代码:

#gear_icon{
background-color: green;
width: 25px;
height: 29px;
cursor: pointer;
border: none;
margin-top: 4px;
margin-bottom: 2px; 
position:absolute;
}
#gear_div{
cursor:default; 
display: none;
/*opacity: 0; $('#gear_div').css({ opacity: 0.0});*/
background-color: #AFD824;
width: 308px;
height: 26px;
position: relative;
top: 0;
left: 27px;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
}

Try adding 尝试添加

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

I'm still trying to figure out why it works like this:) 我还在试图找出它为什么这样工作:)

Fiddle . 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM