Javascript TypeError:undefined不仅仅是Safari和iOS中的对象
我下面的代码在Chrome中运行良好,但在Safari中出现以下错误。有什么解决办法吗Javascript TypeError:undefined不仅仅是Safari和iOS中的对象,javascript,jquery,ios,safari,scrolltop,Javascript,Jquery,Ios,Safari,Scrolltop,我下面的代码在Chrome中运行良好,但在Safari中出现以下错误。有什么解决办法吗 jQuery('.mk-responsive-nav > li > a').click( function() { var href = jQuery(this).attr('href').replace('#', ''); jQuery(window).scrollTop( jQuery("section[data-anchor='" + href + "']").offset().
jQuery('.mk-responsive-nav > li > a').click( function() {
var href = jQuery(this).attr('href').replace('#', '');
jQuery(window).scrollTop( jQuery("section[data-anchor='" + href + "']").offset().top );
console.log( jQuery("section[data-anchor='" + href + "']").offset().top );
});
Safari错误:
TypeError:undefined不是一个对象(计算'jQuery('section[data anchor='“+href+'”]).offset().top')很难说,因为我看不到上下文html。我建议在滚动
窗口之前,尝试捕获href
变量,并确保它实际上是jQuery对象。我确实犯了和你一样的错误
$(函数(){
jQuery('.mk responsive nav>li>a')。单击(函数(){
var href=jQuery(this.attr('href').replace('#','');
var target=jQuery(“section[data-anchor='”+href+']);
log(target.offset();
if(目标长度){
console.log('滚动到:',target.offset().top);
jQuery(window).scrollTop(target.offset().top);
}
});
});代码>
-
-
第一节。
第2节。