简体   繁体   English

jQuery:单击父元素时,隐藏子元素。 单击子元素时,不要隐藏它

[英]jQuery: When parent element is clicked, hide child element. When child element is clicked, don't hide it

I have a parent element with a child element that is displayed/hidden by clicking a specific button on the page. 我有一个父元素和一个子元素,通过单击页面上的特定按钮可以显示/隐藏该子元素。 The button just changes the style from display:none to display:block, pretty simple. 该按钮只是将样式从display:none更改为display:block,非常简单。 The child element starts off as display:none; 子元素以display:none开始;

I want to hide this element when the parent element is clicked, but not when the child element is clicked, which is problematic since the child element is apart of the parent element. 我想在单击父元素时隐藏此元素,但在单击子元素时不希望隐藏,这是有问题的,因为子元素父元素的一部分。

I saw that there was data(); 我看到有data(); that can check if an element was clicked, but that doesn't seem to be working. 可以检查是否单击了某个元素,但这似乎不起作用。

Here is the html: 这是html:

<style>

#parent {width:200px;height:200px;}
#child {display:none;width:50px;height:50px;}
#child.display {display:block;}

</style>

<div id="parent">
Some content
    <div id="child">Some other content</div>
</div>

<button id="clickMe">Toggle Child</button>

And here is the jQuery: 这是jQuery:

$(function() 
{
  $('#clickMe').click(function() 
  { 
      $('#child').toggleClass('display');
  }); 

  $('#parent').click(function()
  {
     if($('#child').data('clicked') == true )
    {
        // do nothing
    } else 
    {
      $('#child').toggleClass('display');
    }
  });
});

For the sake of this example I hid the child element by default. 为了这个示例,默认情况下我隐藏了child元素。 Don't worry, I won't be doing that in production. 不用担心,我不会在生产中这样做。 I'm just working on figuring out how to do this base functionality. 我正在研究如何执行此基本功能。

Here is a link to demo of it: jsfiddle 这是它的演示链接: jsfiddle

You can accomplish that by binding an event to the child and stop the propagation. 您可以通过将事件绑定到子级并停止传播来实现。

  $('#parent').click(function() {
     $('#child').toggleClass('display');
  });
  $('#child').click(function(e) {
     e.stopPropagation();
  });

More information can be found here 更多信息可以在这里找到

Updated Fiddle 更新小提琴

Here's an updated fiddle with the problem resolved: https://jsfiddle.net/6u39dfxd/1/ 这是一个已解决问题的更新小提琴: https : //jsfiddle.net/6u39dfxd/1/

$('#clickMe').click(function() { 
    $('#child').toggleClass('display');
}); 

$('#parent').click(function(event) {
    if($(this).has(event.target).length === 0) {
        $('#child').toggleClass('display');
    }
});

Basically, you get the event.target on click and ensure that it's not contained within the parent element. 基本上,您会在单击时获得event.target,并确保其未包含在父元素中。 You could get more specific if there's more than one child, but this should resolve it. 如果有一个以上的孩子,您可能会更加具体,但这应该可以解决。

  $('#clickMe').click(function() 
  { 
      $('#child').toggle();
  }); 

  $('#parent').click(function(e)
  {
    $('#child').toggle();      
  });
    $("#parent").children().click( function(e) {
    e.stopPropagation();
  });

I think this will solve your problem. 我认为这可以解决您的问题。

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

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