獲得一個.TOP未定義或爲空的錯誤在IE9以下代碼:.TOP jQuery的問題與空.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;
}
}
});
請幫幫忙!基本上,菜單應該能夠點擊到「單頁」網站上的適當部分。但是也有單個博客文章的子頁面。在這些單獨的博客帖子頁面上,當點擊菜單項時,會引發錯誤。我的看法是,它與這些子頁面上沒有param(也不是空白參數)有關,而是一個子頁面url。思考?
'的console.log(jQuery的( '#' + PARAM)。長度)' –
中頻由Kevin B發佈的代碼=== 0,那麼'offset()'是未定義的。基本上確保你的參數是有效的。 –
內置了'location.hash'這樣的東西? – adeneo