2014-07-10 64 views
1

我需要實現點擊外到內隱藏下拉指令,所以我有這樣的代碼:如何角元素比較jQuery的元素

$(document).click(function(e) { 
     var selector = $(e.target).closest('.time-selector'); 
     if (!selector.length) { // || !selector.is(element)) { 
      if ($scope.$$phase) { 
       $scope.show = false; 
      } else { 
       $scope.$apply(function() { 
        $scope.show = false; 
       }); 
      } 
     } 
    }); 

,但它不工作(不隱藏),當我點擊不同的.timer-selector元素,我試着測試!selector.is(element)但這不起作用。

那麼如何測試我的jQuery選定元素是否與角度指令元素相同的DOM節點?

不知道這是相關的,但我的指令沒有replace

回答

2

找到解決辦法,在我的情況,我的元素是我的指令標籤,而不是.time-selector,所以這段代碼解決這個問題:

$(document).click(function(e) { 
     var selector = $(e.target).closest('.time-selector'); 
     if (!element.find('.time-selector').is(selector)) { 
      if ($scope.$$phase) { 
       $scope.show = false; 
      } else { 
       $scope.$apply(function() { 
        $scope.show = false; 
       }); 
      } 
     } 
    }); 
0

試試這個:

$(document).click(function(e) { 
    var selector = $(e.target).closest('.time-selector'); 
    if (!selector.length) { // || !selector.is(element)) { 
     // adjust this line here to get the scope where .show is defined 
     var $scope = selector.scope(); 
     $scope.$apply(function() { 
      $scope.show = false; 
     }); 

    } 
}); 

這可能也取決於是否selector.scope()回報,其中「秀」的定義範圍可能無法正常工作。如果這不起作用,那麼找到定義'show'被定義的範圍的元素,然後在jQuery元素上調用.scope()來獲取範圍。

0

有趣的是「元素」究竟包含什麼。如果是像

<div id="foo"></div> 

您可以檢查不等式

selector[0] != element 

$ jQueryObject [指數]返回相應的HTML元素。

+0

元是我的'',可能是我有問題的代碼將工作時,有是'替換:真'。 – jcubic