2014-09-10 119 views
0

我正在使用AngularJS框架,我在jQuery中使用'hasClass'爲了讓鼠標懸停在下拉菜單上。它在Chrome和FF中工作正常,但在IE中,當我單擊按鈕並導航到另一頁時,它提供了一個錯誤,「無法獲取屬性hasClass的未定義或空引用」。 這個錯誤並不是每次都顯示出來,但它經常出現在IE中。 同樣,有時候不是'hasClass',它會拋出'height'爲未定義或空引用。 這裏是代碼:無法獲取未定義的屬性'hasClass'或空引用

(function ($, window, delay) { 

     var theTimer = 0; 
     var theElement = null; 
     var theLastPosition = { x: 0, y: 0 }; 
     $('[data-toggle]') 
      .closest('li') 
      .on('mouseenter', function (inEvent) { 
       if (theElement) theElement.removeClass('open'); 
       window.clearTimeout(theTimer); 
       theElement = $(this); 

       theTimer = window.setTimeout(function() { 
        theElement.addClass('open'); 
       }, delay); 
      }) 
      .on('mousemove', function (inEvent) { 
       if (Math.abs(theLastPosition.x - inEvent.ScreenX) > 4 || 
       Math.abs(theLastPosition.y - inEvent.ScreenY) > 4) { 
        theLastPosition.x = inEvent.ScreenX; 
        theLastPosition.y = inEvent.ScreenY; 
        return; 
       } 

       if (theElement.hasClass('open')) return; 
       window.clearTimeout(theTimer); 
       theTimer = window.setTimeout(function() { 
        theElement.addClass('open'); 
       }, delay); 
      }) 
      .on('mouseleave', function (inEvent) { 
       window.clearTimeout(theTimer); 
       theElement = $(this); 
       theTimer = window.setTimeout(function() { 
        theElement.removeClass('open'); 
       }, delay); 
      }); 
    })(jQuery, window, 50); // 50 is the delay in milliseconds 

有什麼建議嗎? 謝謝。

+1

我們怎麼沒有看到任何代碼暗示什麼嗎? – adeneo 2014-09-10 23:24:13

+0

@Oriol:謝謝,你能提供更新的代碼嗎? – Madhav 2014-09-10 23:37:56

+0

應該把這個角度指令 – charlietfl 2014-09-10 23:53:23

回答

0

可能的問題是mousemove事件在某些情況下可能會早於mouseenter觸發。

然後,更好地檢查是否theElement仍然nullmousemove事件偵聽器:

.on('mousemove', function (inEvent) { 
    /* ... */ 
    if (!theElement || theElement.hasClass('open')) { return; } 
    window.clearTimeout(theTimer); 
    theTimer = window.setTimeout(function() { 
     theElement.addClass('open'); 
    }, delay); 
}) 
+0

謝謝Oriol。有用。! – Madhav 2014-09-11 01:12:11

+0

@Maddy如果它的作品可以隨意標記爲接受的答案:)關於另一個問題,這意味着'grid。$ topPanel'是未定義的。我不知道angularjs和它的插件,所以我無法提供更多幫助。最好在另一個問題上提出。 – Oriol 2014-09-11 01:25:14

相關問題