简体   繁体   English

当div失去焦点时如何关闭div?

[英]How to close div when div loses focus?

I made a simple plunkr here http://plnkr.co/edit/zNb65ErYH5HXgAQPOSM0?p=preview 我在这里做了一个简单的plunkr http://plnkr.co/edit/zNb65ErYH5HXgAQPOSM0?p=preview

I created a little datepicker I would like this to close itself when you focus out of it (focusout of datepicker) if I put blur on input I'm unable to use the datepicker, if I put focusout event on datepicker it doesn't works 我创建了一个小日期选择器,如果我对输入进行模糊处理,我希望它在关闭时自动关闭(日期选择器的聚焦),但是我无法使用日期选择器,如果我将焦点输出事件放在日期选择器上,它将无法正常工作

I also tried: 我也尝试过:

angular.element(theCalendar).bind('blur', function () {
    $scope.hideCalendar();
});

but it doesn't work. 但这不起作用。

Any clue? 有什么线索吗?

this is because you are removing the item before you get a chance to do anything, here is a working example: 这是因为您在有机会做任何事情之前就删除了该项目,这是一个有效的示例:

http://plnkr.co/edit/mDfV9NLAQCP4l7wHdlfi?p=preview http://plnkr.co/edit/mDfV9NLAQCP4l7wHdlfi?p=preview

just add a timeout: 只需添加一个超时:

thisInput.bind('blur', function () {
  $timeout(function(){
    $scope.hideCalendar();
  }, 200);
});

have you considered using existing datepickers? 您是否考虑过使用现有的日期选择器? like angularUI or angular-strap: http://mgcrea.github.io/angular-strap/##datepickers 像angularUI或angular-strap: http ://mgcrea.github.io/angular-strap/##datepickers

Update: 更新:

Not a complete solution, but should get you quite closer: 不是一个完整的解决方案,但应该可以使您更加接近:

    angular.element($document[0].body).bind('click', function(e){
      console.log(angular.element(e.target), e.target.nodeName)
      var classNamed =  angular.element(e.target).attr('class');
      var inThing = (classNamed.indexOf('datepicker-calendar') > -1);
      if (inThing || e.target.nodeName === "INPUT") {
        console.log('in');
      } else {
        console.log('out');
          $timeout(function(){
            $scope.hideCalendar();
          }, 200);
      }
    });

http://plnkr.co/edit/EbQl5xsCnG837rAEhBZh?p=preview http://plnkr.co/edit/EbQl5xsCnG837rAEhBZh?p=preview

What you want to do then is to listen for a click on the page, and if the click is outside of the calendar, then close it, otherwise do nothing. 然后,您要做的就是监听页面上的单击,如果单击不在日历中,则将其关闭,否则不执行任何操作。 The above only takes into account that you are clicking on something that has a class name which includes datepicker-calendar, you will need to adjust it so that clicking within the calendar doesn't close it as well. 上面仅考虑到您单击的类名称包括datepicker-calendar的东西,您需要对其进行调整,以便在日历中单击也不会将其关闭。

How about closing on mouseout? 鼠标关闭时如何关闭?

You need to cancel the close if you move to another div in the calendar though: 但是,如果您移至日历中的另一个div,则需要取消关闭:

    //get the calendar as element
    theCalendar = element[0].children[1];

    // hide the calendar on mouseout
    var closeCalendarTimeout = null;
    angular.element(theCalendar).bind('mouseout', function () {
      if ( closeCalendarTimeout !== null )
        $timeout.cancel(closeCalendarTimeout);
      closeCalendarTimeout = $timeout(function () {
        $scope.hideCalendar();
      },250)
    });
    angular.element(theCalendar).bind('mouseover', function () {
      if ( closeCalendarTimeout === null ) return
      $timeout.cancel(closeCalendarTimeout);
      closeCalendarTimeout = null;
    });

EDIT 编辑

Adding a tabindex attribute to a div causes it to fire focus and blur events. 向div添加tabindex属性会导致其触发焦点并模糊事件。

, htmlTemplate = '<div class="datepicker-calendar" tabindex="0">' +

angular.element(theCalendar).bind('blur', function () {
  $scope.hideCalendar();
});

So, i know it probably is not the best practice or the best way to do this, but at the end i fixed and got what i need using this: 因此,我知道这可能不是最佳实践或最佳方法,但最后我固定并获得了使用此方法所需的功能:

thisInput.bind('focus click', function bindingFunction() {

          isMouseOnInput = true;
          $scope.showCalendar();
          angular.element(theCalendar).triggerHandler('focus');
        });
        thisInput.bind('blur focusout', function bindingFunction() {

          isMouseOnInput = false;
        });

        angular.element(theCalendar).bind('mouseenter', function () {

          isMouseOn = true;
        });

        angular.element(theCalendar).bind('mouseleave', function () {

          isMouseOn = false;
        });

        angular.element($window).bind('click', function () {

          if (!isMouseOn && !isMouseOnInput) {

            $scope.hideCalendar();
          }
        });

I setted up some boolean vars to check where mouse is when you click the page and it works like a charm if you have some better solution that works , please let me know, but this actually fixed all. 我设置了一些布尔变量,以检查单击页面时鼠标的位置,如果您有更好的解决方案可以工作,它就像一个超级按钮,请让我知道,但这实际上已解决了所有问题。

I accept this as the answer but i thank all the guys on this page! 我接受这个作为答案,但是我感谢本页上的所有人!

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

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