2014-09-10 54 views
0

我在這裏做一個簡單的plunkr http://plnkr.co/edit/zNb65ErYH5HXgAQPOSM0?p=preview如何在div丟失焦點時關閉div?

我創建了一個小日期選擇器,我想這當你專注出來的(事件的內容的日期選擇器),如果我把模糊輸入我無法使用日期選擇器本身關閉如果我把事件的內容事件的日期選擇器不工作

我也試過:

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

,但它不工作。

任何線索?

+0

您是否嘗試過輸入ng-blur指令? – SoluableNonagon 2014-09-10 15:47:43

+0

另外,你的plunker是完整的錯誤,請解決這些,以便其他人可以幫助你。 – SoluableNonagon 2014-09-10 15:48:57

+0

@EliteOctagon對不起,這是一箇舊版本的角度引起的錯誤,修正:http://plnkr.co/edit/zNb65ErYH5HXgAQPOSM0?p=preview – sbaaaang 2014-09-10 15:59:46

回答

0

所以,我知道這可能不是最好的做法還是要做到這一點的最好辦法,但最後我固定,並得到了什麼,我需要使用這樣的:

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(); 
      } 
     }); 

我設置了一些布爾變量來檢查鼠標在你點擊頁面時的位置,如果你有一些更好的解決方案可以工作,它就像一個魅力,請讓我知道,但這實際上是固定的。

我接受這個答案,但我感謝這個頁面上的所有人!

1

這是因爲你刪除的項目,你有機會做任何事情之前,這裏是一個工作示例:

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

只需添加超時:

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

有你考慮使用現有的日期選擇器?像angularUI或角帶:http://mgcrea.github.io/angular-strap/##datepickers

更新:

不是一個完整的解決方案,但應該讓你很接近:

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

你想要做的又是什麼來聽取頁面上的點擊,如果點擊在日曆之外,請關閉它,否則什麼也不做。以上只考慮到您點擊某個類名包含datepicker-calendar的東西,您需要對其進行調整,以便日曆內的點擊不會將其關閉。

+0

我在問題中指定如果我使用輸入模糊我無法使用日期選擇器,請嘗試單擊一年選擇一年;) – sbaaaang 2014-09-10 15:53:03

+0

我不使用角度-UI我不需要它對不起:) – sbaaaang 2014-09-10 15:53:25

+0

我想隱藏datepicker時datepicker是模糊不輸入;) – sbaaaang 2014-09-10 15:56:11

1

如何關閉mouseout?

您需要取消關閉,如果您在日曆中移動到另一個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; 
    }); 

編輯

添加的tabindex屬性來一個div導致其火焦點和模糊事件。

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

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

我appriciated謝謝,但對不起,我希望用戶能夠看到datepicker也如果mouseout,我只是希望它關閉它的點擊ousidet:P – sbaaaang 2014-09-10 17:39:09

+0

好吧,然後看到我的答案中的新建議(設置tabindex屬性上日曆)。 – marneborn 2014-09-10 18:08:20

+0

@mameborn +1爲酷tabindex attr我dint知道,如果只有我可以採取div自動對焦它將是明確的使用tabindex! – sbaaaang 2014-09-10 19:40:54