简体   繁体   English

jQuery切换div,允许在div外部单击以关闭,也允许div内部的所有内容都可点击

[英]Jquery toggle div, allow clicking outside of div to close, also allow everything inside the div clickable

I have a toggle event on a div, and I've seen many questions regarding mine but nothing seems to work for what I'm trying to do, it either makes my div disappear in a few seconds, or it just doesn't work. 我在div上有一个切换事件,并且我已经看到很多有关我的问题,但似乎对我要执行的操作没有任何作用,它会使我的div在几秒钟后消失,或者它不起作用。 Here's my jsfiddle . 这是我的jsfiddle

I have a div that needs to toggle when another <div> is clicked. 我有一个div,当单击另一个<div>时需要切换。 The toggled div has inputs in it that need to be filled out, and a submit button inside it as well. 切换后的div中有需要填写的输入,内部也有一个提交按钮。 So I need clicks inside the div to be allowed, but only inside my div. 因此,我需要允许在div内单击,但仅在我的div内。 So I want the div to show unless the user clicks outside of this div. 因此,除非用户在此div之外单击,否则我希望显示div。

I'm using this query which toggles fine: 我正在使用此查询,可以很好地切换:

$('#MyDiv').click(function (event) {
  $("#ToggledDiv").slideToggle();
});

And then this coding to hide it when clicked outside of the div which doesn't work: 然后在div外部单击时无法隐藏的以下编码:

$(window).click(function () {
  $("ToggledDiv").hide();
});

I've tried solutions with e.preventDefault(); 我已经尝试过使用e.preventDefault(); but that doesn't work, or $(document).click, even mousedown but it just doesn't flow how I want, it'll hide it within a few seconds, or it will prevent the toggle from even working so I'm lost. 但这不起作用,或者$(document).click甚至没有mousedown都行不通,但是它并没有达到我想要的效果,它会在几秒钟内将其隐藏起来,否则它将阻止切换功能正常工作,所以我我迷路了。

Take a look at the event when you click inside the #targetDiv . #targetDiv内单击时,请查看event There are two properties you can use to evaluate what action to perform: event.target and event.currentTarget . 您可以使用两个属性来评估要执行的操作: event.targetevent.currentTarget In this case: 在这种情况下:

$('#ToggledDiv').on('click', function(event) {
    console.log(event.target, event.currentTarget);
});

This is a good way to see if what clicked is actual target or a child element in the target. 这是查看单击的是实际目标还是目标中的子元素的好方法。

To add to Chris' answer, you can see here that I check that the e.target is not inside the form using vanilla Node.contains , and also not the button... 要添加到克里斯的答案中,您可以在这里看到我使用香草Node.contains检查e.target是否不在表单内,也没有按钮...

https://jsfiddle.net/jmLdp45s/3/ https://jsfiddle.net/jmLdp45s/3/

var $button = $('button');
var $form = $('form');

$button.click(function() {
  $form.slideToggle();
});

$(window).click(function(event) {
  if (
    !$form.get(0).contains( event.target ) // target is not inside form
    && event.target !== $button.get(0) // target is not button
  ) $form.hide();
});

The reason behind this behavior is Event Bubbling and Capturing of HTML DOM API. 此行为背后的原因是HTML DOM API的事件冒泡和捕获

You can use event.stopPropagation() OR event.cancelBubble = true to prevents the event from bubbling up to the DOM tree, preventing any parent handlers from being notified of the event. 您可以使用event.stopPropagation()event.cancelBubble = true来防止事件冒泡到DOM树,从而防止任何父处理程序收到该事件的通知。

Another good article: events order 另一篇好文章: 事件顺序

 $('#MyDiv').click(function(event) { $("#ToggledDiv").show(); disabledEventPropagation(event); //console.log('2nd event'); }); $('#ToggledDiv').click(function(event) { disabledEventPropagation(event); //console.log('3rd event'); }); $(document).click(function() { $("#ToggledDiv").hide(); //console.log('1st event'); }); function disabledEventPropagation(event) { if (event.stopPropagation) { event.stopPropagation(); } else if (window.event) { window.event.cancelBubble = true; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="MyDiv" style="background-color:yellow"> click me to open </div> <div id="ToggledDiv" style="display: none;background-color:yellow"> <input type="text" /> <input type="submit" /> </div> 

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

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