.top jQuery问题,返回null.top
在ie9中获取以下代码的.top未定义错误或null错误:.top jQuery问题,返回null.top,jquery,Jquery,在ie9中获取以下代码的.top未定义错误或null错误: 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
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;
}
}
});
jQuery('#menu').menu();
jQuery(“#菜单”)。查找('a')。单击(函数(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;
函数cleanURL(url){
if(url.match(/http:\/\/))
{
url=url.substring(7);
}
如果(url.match(/^www\./))
{
url=url.substring(4);
}
返回url;
}
$go_to=cleanURL($go_to_url);
$current\u url=cleanURL($current\u url);
if(jQuery(this).closest('#menu').hasClass('render')){
如果(参数){
e、 预防默认值();
if(jQuery('#'+param).length>0){
if(jQuery(window).width()>767){
if(jQuery('.header').hasClass('sticky')){
scroll_distance=scroll_distance-jQuery('.header').outerHeight();
}否则{
滚动距离=滚动距离;
}
}
如果($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\u distance},0);
});
}
否则{
window.location=$go_to_url+'#'+param;
}
}否则{
window.location=$go_to_url+'#'+param;
}
}否则{
window.location=$go\u to\u url;
}
}
});
请帮忙!基本上,菜单应该能够点击到“一页”网站上的适当部分。但是也有用于单个博客帖子的子页面。在这些博客文章页面上,单击菜单项时,会抛出错误。我认为这与子页面上没有参数(也没有空白参数)有关,而是与子页面url有关。想法?我建议替换这一行:
var scroll_distance=jQuery('#'+param).offset().top代码>
比如说:
var scroll_distance=jQuery('#'+param).length?jQuery('#'+param).offset().top:0代码>
如果jQuery(“#”+param)
不返回任何对象,则.offset()
未定义
通过这种方式,您可以测试是否定义了滚动距离
,并且仅在以下情况下尝试其他操作:
if (scroll_distance) {
//your logic goes here
}
console.log(jQuery('#'+param.length)
如果Kevin B发布的代码===0,则offset()
未定义。基本上确保你的参数是有效的。有内置的location.hash
之类的东西吗?我把它添加到我的js文件中,它根本没有记录任何东西。只是为了提供一些背景,我正在定制这个主题:。如果你点击一个博客页面,然后点击一个导航项目,它会在现代浏览器中抛出点击错误,但会在ie9.Ah中弹出。我把上面的代码放错地方了。移动了它,它现在记录为“1”