简体   繁体   English

单击鼠标隐藏div

[英]Hide div on mouse click

I've created dialog windows; 我已经创建了对话框窗口; and I want them to be closed whenever the uses clicks anywhere else than the dialog window buttons (hyperlinks). 而且我希望每当用户单击对话框窗口按钮(超链接)以外的任何位置时,将它们关闭。

I was creating one large "overlay" div (0 opacity) at the back of dialog window to catch the clicks, but that becomes quite problematic when the user wants to clck anything at the back (like another hyperlink) as well as closing the dialog at the same time. 我在对话框窗口的后面创建了一个大的“叠加” div(0透明度)来捕获点击,但是当用户想要在后面敲东西(如另一个超链接)以及关闭对话框时,这变得很成问题与此同时。 Since there is overlay, it becomes activated (to close the dialog) and the clicked hyperlink is not catched. 由于存在叠加层,因此它会被激活(关闭对话框),并且不会捕获单击的超链接。

Shortly, I need a solution for this situation where I'll close the dialog windows whenever user does anything except clicking the dialog window buttons. 很快,对于这种情况,我需要一个解决方案,在这种情况下,只要用户执行任何操作(单击对话框窗口按钮除外),我都会关闭对话框窗口。

I hope it's clear; 我希望这很清楚。 thank you. 谢谢。

This is being caused by event bubbling . 这是由事件冒泡引起的。 It's a shame that 2 people downvoted @Lilith2k3 answer because he wasn't wrong and @Xotic750 had way too complex a solution. 遗憾的是,有2个人对@ Lilith2k3的回答不满意,因为他没错,而@ Xotic750的解决方案太复杂了。 You do need an event handler on your body, but you need simply to filter out clicks from the your dialog. 确实需要一个事件处理程序,但是您只需要过滤掉对话框中的点击即可。

You need two onclick() handlers. 您需要两个onclick()处理函数。 One in your body to close the dialog, and the other in your dialog to cancel event bubbling. 您一个人关闭对话框,另一个人取消事件冒泡。 See below 见下文

function dlgClickHandler(e) {
    // do dialog stuff, then...
    e.cancelBubble = true; if (e.stopPropagation) e.stopPropagation(); // cancel event bubbling so body click handler not activated
}

function bodyClickHandler(e) {
    // close dlg
}

This is also the reason you can't do this simply by comparing the ID of the dialog, because the click may have come from one of the children (eg. your OK, cancel buttons). 这也是您不能仅通过比较对话框ID来完成此操作的原因,因为单击可能来自其中一个子项(例如,您的“确定”,“取消”按钮)。

I've wrapped the functions in a module pattern to make it a neater component, and although I've used jQuery in the first example (which I suspect you are not) the technique pre-dates jQuery. 我将功能包装在模块模式中,使其成为更整洁的组件,尽管在第一个示例中使用了jQuery(我怀疑您不是),但该技术早于jQuery。

One of the reasons I suspect you're not using jQuery, is because if you were you'd probably already have stumbled across one of the many jQuery popup plugins for handing dialogs like this. 我怀疑您不使用jQuery的原因之一是因为如果您使用的话,可能已经偶然发现了许多用于处理此类对话框的jQuery弹出插件之一。 If you've not tried jQuery take a look, it might help you out in many other ways too. 如果您还没有尝试过jQuery,它可能也会以许多其他方式帮助您。

This is a very basic demonstration. 这是一个非常基本的演示。 We have a yellow div on the screen that represents your dialog. 屏幕上的黄色div代表您的对话框。 If you click anywhere in the div then it remains visible, you could fill this div with more HTML and event handlers to do as you wish. 如果你点击任何地方div那么它仍然可见,你可以填补这个div更多的HTML和事件处理程序做你的愿望。 Click anywhere outside of the div and the div will become hidden. 单击div之外的任何位置, div将被隐藏。 Note: I am not cleaning up any event handlers, which you will want to do. 注意:我不会清理任何事件处理程序,您将要做。

Please see the answer by cirrus , where he actually gives an explanation about event propagation and why you will need it in your solution, which I haven't done here. 请参阅cirrus答案 ,他实际上在其中解释了事件传播以及为什么您需要在解决方案中使用它,我在这里还没有做过。 He also gives you a solution using vanilla javascript and jquery , which I don't. 他还为您提供了使用香草javascript和jquery的解决方案,而我没有。 He also demonstrates the javascript module pattern where I have not. 他还演示了我没有的javascript模块模式 I wouldn't have been able to bring you this answer without his constructive critisism and tuition , which prompted me to come back here and improve on my original poor, time constrained answer. 没有他的建设性批评和学费 ,我将无法为您提供这个答案,这促使我回到这里,改进了我最初的,受时间限制的可怜答案。 Good luck. 祝好运。

CSS 的CSS

.box {
    width:300px;
    height:100px;
    position: absolute;
    top: 30%;
    left: 30%;
    background-color:yellow;
    border:2px solid;
}
#message {
    position: absolute;
    right: 50%;
    bottom: 50%;
}
#button1 {
    position: absolute;
    right: 0;
    bottom: 0;
}
#button2 {
    position: absolute;
    right: 4em;
    bottom: 0;
}

HTML 的HTML

<div id="box" class="box">
    <div id="message"></div>
    <button id="button1">
        <img src="http://img856.imageshack.us/img856/3817/ticklf.png" alt />Ok</button>
    <button id="button2">
        <img src="http://img822.imageshack.us/img822/1917/crossn.png" alt />Cancel</button>
</div>

JavaScript 的JavaScript

function dontBubble(evt) {
    evt.stopPropagation();
}

function hideBox(evt) {
    box.hidden = true;
}

function messgage() {
    document.getElementById("message").textContent = "I'm ignoring you";
}

document.getElementById("box").addEventListener("click", dontBubble, false);
document.getElementById("button1").addEventListener("click", messgage, false);
document.getElementById("button2").addEventListener("click", hideBox, false)
document.addEventListener("click", hideBox, false)

; ;

On jsfiddle jsfiddle上

充其量,您可以将$ click事件绑定到$(“ body”),后者检查用户单击的位置,如果用户不在对话框中单击,则可以取消绑定该事件并关闭对话框。

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

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