简体   繁体   English

当失去焦点/模糊时隐藏DIV

[英]Hide a DIV when it loses focus/blur

我有一个显示DIV的JavaScript(将其显示css属性从'none'设置为'normal'。有没有办法让它集中注意力,以便当我点击页面上的其他地方时,DIV失去焦点,它display属性设置为none(基本上隐藏它)。我正在使用JavaScript和jQuery

For the hide the div when clicking any where on page except the selecteddiv 对于除了selecteddiv之外的任何页面上的任何位置隐藏div

$(document).not("#selecteddiv").click(function() {
        $('#selecteddiv').hide();
    });

if you want to hide the div with lost focus or blur with animation then also 如果你想隐藏焦点丢失的div或动画模糊那么

$("#selecteddiv").focusout(function() {
        $('#selecteddiv').hide();
    });

with animation 用动画

$("#selecteddiv").focusout(function() {
    $('#selecteddiv').animate({
        display:"none"
    });
});

May this will help you 愿这对你有所帮助

The examples already given unfortunately do not work if you have an iframe on your site and then click inside the iframe. 不幸的是,如果您的网站上有iframe,然后在iframe内部点击,那么已经提供的示例无效。 Attaching the event to the document will only attach it to same document that your element is in. 将事件附加到文档只会将其附加到元素所在的同一文档中。

You could also attach it to any iframes you're using, but most browsers won't let you do this if the iframe has loaded content from another domain. 您也可以将其附加到您正在使用的任何iframe,但如果iframe已从其他域加载内容,则大多数浏览器都不允许您这样做。

The best way to do this is to copy what's done in the jQuery UI menubar plugin. 执行此操作的最佳方法是复制jQuery UI菜单栏插件中完成的操作。

Basic example HTML: 基本示例HTML:

<div id="menu">Click here to show the menu
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li><a href="#">Item 3</a></li>
    </ul>
</div>

And the jQuery needed to make it work: 而jQuery需要让它工作:

var timeKeeper;

$('#menu').click(function()
{
    $('#menu ul').show();
});

$('#menu ul').click(function()
{
    clearTimeout(timeKeeper);                  
});

$('#menu').focusout(function()
{
    timeKeeper = setTimeout(function() {$('#menu ul').hide()}, 150);
});

$('#menu').attr('tabIndex', -1);
$('#menu ul').hide();

What it does is give the menu a tab index, so that it can be considered to have focus. 它的作用是给菜单一个标签索引,以便可以认为它具有焦点。 Now that you've done that you can use the focusout event handler on the menu. 既然你已经完成了,你可以在菜单上使用focusout事件处理程序。 This will fire whenever it has been considered to lose focus. 只要被认为失去焦点,就会触发。 Unfortunately, clicking some child elements will trigger the focusout event (example clicking links) so we need to disable hiding the menu if any child elements have been clicked. 不幸的是,单击某些子元素将触发焦点事件(例如单击链接),因此如果单击任何子元素,我们需要禁用隐藏菜单。

Because the focusout event gets called before the click event of any children, the way to achieve this is by setting a small timeout before hiding the element, and then a click on any child elements should clear this timeout, meaning the menu doesn't get hidden. 因为在任何子项的click事件之前调用focusout事件,实现此目的的方法是在隐藏元素之前设置一个小超时,然后单击任何子元素应清除此超时,这意味着菜单不会隐。

Here is my working jsfiddle example 这是我工作的jsfiddle示例

$(document).mouseup(function (e)
{
    var container = $("YOUR CONTAINER SELECTOR");

   if (!container.is(e.target)&& container.has(e.target).length === 0) 
   {
      container.hide();
   }
});

Regarding mouse clicks, see the other answers. 关于鼠标点击,请参阅其他答案。

However regarding lost focus, .focusout is not the event to attach to, but rather .focusin . 然而,对于失去焦点, .focusout不是要附加的事件,而是.focusin Why? 为什么? Consider the following popup: 请考虑以下弹出窗口:

<div class="popup">
  <input type="text" name="t1">
  <input type="text" name="t2">
</div>

What happens on moving from t1 to t2: 从t1转到t2会发生什么:

  • t1 sends focusout , which bubbles up to $('.popup').focusout t1发送focusout ,它会冒泡到$('.popup').focusout
  • t2 sends focusin , which bubbles up to $('.popup').focusin t2发送focusin ,冒泡到$('.popup').focusin

... so you get both types of event even though the focus stayed completely inside the popup. ...所以你得到两种类型的事件,即使焦点完全保留在弹出窗口内。

The solution is to analogous to the magic trick done with .click : 解决方案类似于使用.click完成的魔术:

$(document).ready(function() {
    $('html').focusin(function() {
        $('.popup').hide(); 
    });
    $('.popup').focusin(function(ev) {
        ev.stopPropagation(); 
    });
});

(side note: I found the .not(...) solution not working bc. of event bubbling). (旁注:我发现.not(...)解决方案无法正常运行事件冒泡)。

Bonus: working fiddle click me - open the popup, then try tabbing through the inputs. 奖金:工作小提琴点击我 - 打开弹出窗口,然后尝试通过输入选项卡。

You can bind a function on click of body and check if its the current div using e.target (e is the event) 你可以点击body来绑定一个函数,并使用e.target检查它是否是当前的div(e是事件)

$(document).ready(function () {
  $("body").click(function(e) {     
    if($(e.target).attr('id') === "div-id") {
        $("#div-id").show();
    }
    else {
        $("#div-id").hide();
    }
  });
});

I was also looking for this and here I found the solution https://api.jquery.com/mouseleave/ . 我也在寻找这个,在这里我找到了解决方案https://api.jquery.com/mouseleave/ This may be useful for future readers. 这对未来的读者可能有用。

The mouseleave event differs from mouseout in the way it handles event bubbling. mouseleave事件与mouseout处理事件冒泡的方式不同。 If mouseout were used in this example, then when the mouse pointer moved out of the Inner element, the handler would be triggered. 如果在此示例中使用了mouseout,那么当鼠标指针移出Inner元素时,将触发处理程序。 This is usually undesirable behavior. 这通常是不受欢迎的行为。 The mouseleave event, on the other hand, only triggers its handler when the mouse leaves the element it is bound to, not a descendant. 另一方面,mouseleave事件仅在鼠标离开它所绑定的元素时触发其处理程序,而不是后代。

$('.menu > li').click(function() {
    $(this).children('ul').stop().slideDown('fast',function()
    {
        $(document).one('click',function()
        {
            $('.menu > li').children('ul').stop().slideUp('fast');
        });
    });

});

On triggering mouseup() event, we can check the click is inside the div or a descendant and take action accordingly. 在触发mouseup()事件时,我们可以检查click是在div或者后代内部并相应地采取行动。

$(document).mouseup(function (e) {
    var divContent= $(".className");
    if(!divContent.is(e.target) && divContent.has(e.target).length === 0) {
        $(".className").hide();
    }
});

I personally haven't tried blur on divs, only on inputs etc. If blur eventhandler works, it's perfect and use it. 我个人没有尝试模糊div,只在输入等。如果模糊eventhandler工作,它是完美的并使用它。 If it doesn't, you could check this out: jQuery animate when <div> loses focus 如果没有,你可以检查一下: 当<div>失去焦点时jQuery动画

Showing is easy 显示很容易

$('somewhere').click(function {$('#foo').show();})

For hiding 为了隐藏

How do I hide a div when it loses its focus? 当它失去焦点时如何隐藏div?

With jQuery you can hide elements with hide() , ex: $("#foo").hide() 使用jQuery,你可以使用hide()隐藏元素,例如: $("#foo").hide()

Hide element in event listener: 隐藏事件侦听器中的元素:

$("#foo").blur(function() {
    $("#foo").hide();
});

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

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