ie9 中为以下代码获取 .top 未定义或空错误:
jQuery('#menu').menu();
jQuery('#menu').find('a').click(function(e){
if(jQuery(window).width() < 767) {
jQuery('#menu').toggleClass('show');
}
var $go_to = jQuery(this).attr('href');
var param = $go_to.split('#')[1];
var $go_to_url = $go_to.split('#')[0];
var $current_url = window.location.href.split('#')[0];
var scroll_distance = jQuery('#'+param).offset().top;
function cleanURL(url) {
if(url.match(/http:///))
{
url = url.substring(7);
}
if(url.match(/^www./))
{
url = url.substring(4);
}
return url;
}
$go_to = cleanURL($go_to_url);
$current_url = cleanURL($current_url);
if(jQuery(this).closest('#menu').hasClass('render')) {
if(param) {
e.preventDefault();
if(jQuery('#'+param).length > 0) {
if(jQuery(window).width() > 767){
if(jQuery('.header').hasClass('sticky')){
scroll_distance = scroll_distance - jQuery('.header').outerHeight();
} else {
scroll_distance = scroll_distance;
}
}
if($go_to == $current_url) {
jQuery('html, body, document').stop().animate({scrollTop: scroll_distance }, 1000, 'easeOutQuart', function(){
window.location.hash = param;
jQuery('html, body, document').stop().animate({scrollTop: scroll_distance }, 0);
});
}
else {
window.location = $go_to_url+'#'+param;
}
} else {
window.location = $go_to_url+'#'+param;
}
} else {
window.location = $go_to_url;
}
}
});
请帮忙! 基本上,菜单应该能够向下单击到"一页"站点上的适当部分。 但是也有单个博客文章的子页面。 在这些博客文章页面上,单击菜单项时,将引发错误。 我的信念是,这与子页面上没有参数(也不是空白参数)而是子页面网址有关。 思潮?
我建议替换这一行:
var scroll_distance = jQuery('#'+param).offset().top;
像这样:
var scroll_distance = jQuery('#'+param).length ? jQuery('#'+param).offset().top : 0;
如果jQuery('#'+param)
不返回任何对象,则.offset()
未定义。
这样,您就可以测试是否定义了scroll_distance
,并且仅在以下情况下尝试其他操作:
if (scroll_distance) {
//your logic goes here
}