2014-09-10 118 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

相关问题