2017-07-25 70 views
1

这是一个简单的窗口滚动,当用户点击导航项目时触发,但对于我的生活我似乎无法弄清楚为什么FF不会识别它,我读过类似的Q & A的,但他们只推荐定义var的第一个我已经完成,任何帮助将不胜感激。JS在Chrome中正常工作,但不在FireFox

继承人的代码:

$("#myNavbar a").on('click', function(){ 
var hash = this.hash; 

//make sure this.hash has a value 
if (hash !== ""){ 
    //prevent default anchor click behavior 
    event.preventDefault(); 


    //use jQuerys animate() method to add smooth scroll 
    $('html, body').animate({ 
     scrollTop: $(hash).offset().top 
    }, 800, function(){ 

     window.location.hash = hash; 
    }); 
} // end of if 
}); 
+2

事件不会对范围内声明的,也许你缺少$( “#myNavbar A”)。在( '点击',函数(事件){ – juvian

+0

你尝试换'$(文件)。 ready()'围绕你的事件处理函数吗?[jQuery.ready()](https://api.jquery.com/ready/) –

+0

谢谢@juvian这样做。 –

回答

1

这个 “作品” 在Chrome由于Chrome实现了非标准Window.event财产。

如注释中所述,您应该使用由jQuery事件处理程序提供的event参数。

$("#myNavbar a").on('click', function(event){ 
    ... 
    event.preventDefault(); 
    ... 
}); 
+0

干杯noppa,你解决了我的FF难题。 –

相关问题