简体   繁体   English

如果在特定元素之外单击,请删除具有特定类的所有元素

[英]Remove all elements with specific class if clicked outside of specific element

I am working from a continuation of the project that I have started here . 我是从这里开始的项目的继续进行的。

Basically, this is dynamically creating tooltip popups in a relative position to a link when clicked. 基本上,这是在单击链接时在相对位置动态创建工具提示弹出窗口。 What I now need to do is have them close when a click is registered anywhere on the page except for an element with the class of "tooltip-dialog". 我现在需要做的是,在页面上任何位置注册单击时都将其关闭,但“ tooltip-dialog”类的元素除外。 I would like the click to remove all instances of elements with the class of "dialog-anchor" from the DOM. 我想单击以从DOM中删除所有具有“ dialog-anchor”类的元素的实例。 In addition to this, I would like to have only one tooltip popup be allowed active at a time. 除此之外,我想一次只允许一个工具提示弹出窗口处于活动状态。

I played around with this for several hours yesterday evening, but am unsure of how to approach implementing the intended functionality. 我昨天晚上玩了几个小时,但是不确定如何实现预期的功能。 I would really appreciate if someone would be willing to take the time to explain to me how to go about this. 如果有人愿意花时间向我解释如何解决这个问题,我将不胜感激。

Here is the code thus far. 这是到目前为止的代码。 Currently, This only generates the new tooltip on click. 当前,这仅在单击时生成新的工具提示。

$(function() { //jquery document.ready

  $('a.tooltip').on('click', function(e) {
    var $this = $(this);
    e.preventDefault();

    $this.prepend('<div class="dialog-anchor"><div class="dialog-container"><div class="tooltip-dialog"><h4>' + $this.data('title') + '</h4><p>' + $this.data('content') + '</p></div><div class="bg"></div></div></div>');

  });

});

Here's what the on page HTML looks like for the links prior to firing the tooltip popup: 这是触发工具提示弹出窗口之前页面上HTML的外观:

<a class="tooltip top" data-title="This is a tooltip" data-content="blah blah blah blah blah blah blah blah blah"></a>

So you have a relatively-positioned .dialog-anchor element: 因此,您有一个相对定位的.dialog-anchor元素:

$this.prepend('<div class="dialog-anchor">...</div>');

Add an empty dialog-overlay element: 添加一个空的dialog-overlay元素:

$this.prepend('<div class="dialog-overlay"></div>');
$this.prepend('<div class="dialog-anchor">...</div>');

Now, in your CSS: 现在,在您的CSS中:

.dialog-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2;
  background: rgba(0,0,0,1);
}

.dialog-anchor {
  z-index: 3;
}

The dialog box will be above your overlay. 该对话框将在叠加层上方。 The overlay is a transparent layer over your entire screen. 覆盖层是整个屏幕上的透明层。 Add a click-handler to the overlay: 向叠加层添加点击处理程序:

var $overlay = $('<div class="dialog-overlay"></div>');
var $anchor = $('<div class="dialog-anchor">...</div>');
$overlay.on('click', function() {
  $anchor.remove();
});
$this.prepend($overlay);
$this.prepend($anchor);

This solution allows you to do other effects like dimming the rest of the page, like this: 该解决方案允许您执行其他效果,例如使页面的其余部分变暗,如下所示:

background: rgba(0,0,0,0.6);

One option is to bind a listener to the document to catch events bubbling up, check where they came from and see if that is either a target-class element, or inside a target-class element. 一种选择是将侦听器绑定到文档以捕获冒泡的事件,检查事件来自何处,并查看该事件是目标类元素还是目标类元素。

$(document).on('click', function(e) {
    if (!$(e.originalTarget).hasClass('.tooltip-dialog') && !$(e.originalTarget).closest('.tooltip-dialog').length) {
        $('.tooltip-target').remove();
    }
});

The upside of this is that it's guaranteed to catch any click event outside of the tooltip. 这样做的好处是可以确保在工具提示之外捕获任何单击事件。

There are two downsides - first off it will trigger on any click (but click doesn't happen often enough that it's likely to be a problem) and in general, binding events to document should be done sparingly, although in this specific case, it is the correct element to bind it to so it's not actually a big downside either. 有两个缺点-首先,它将在每次单击时触发(但单击的发生频率不足以至于可能会引起问题),通常,应谨慎执行将事件绑定到document操作,尽管在这种情况下,是将其绑定到的正确元素,因此实际上也不是很大的缺点。

Try using the blur() jQuery event. 尝试使用blur() jQuery事件。 It simply does something when the mouse is clicked anywhere outside the element, or even key presses like TAB key http://api.jquery.com/blur/ 只要在元素外部的任何地方单击鼠标,甚至单击TAB键http://api.jquery.com/blur/

 $(".dialog-anchor").not(".tooltip-dialog").blur(function(){
 //Do something
 });

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

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