Javascript 未捕获类型错误:无法读取属性';偏移量';未定义的
我得到未捕获的TypeError:无法读取未定义的属性“offset” 在此之前,我从同一块中得到了相同的“top”异常。我现在用一种方法修复了它,我得到了带有“offset”的错误 这是我的密码Javascript 未捕获类型错误:无法读取属性';偏移量';未定义的,javascript,jquery,Javascript,Jquery,我得到未捕获的TypeError:无法读取未定义的属性“offset” 在此之前,我从同一块中得到了相同的“top”异常。我现在用一种方法修复了它,我得到了带有“offset”的错误 这是我的密码 var ssSmoothScroll = function() { $('.smoothscroll').on('click', function (e) { if (this.hash !== "" && this.pathname == window.lo
var ssSmoothScroll = function() {
$('.smoothscroll').on('click', function (e) {
if (this.hash !== "" && this.pathname == window.location.pathname) {
var target = this.hash;
// $target = $(target);
e.preventDefault();
e.stopPropagation();
var topOffset = 0; //#top should default to 0 so no need to calculate the difference between top and top :)
if (target != "#top") { //If the target is not "#top", then calculate topOffset
var topOffset = $(target).offset().top;
}
}
else{
var target = this.hash,
$target = $(target);
e.preventDefault();
e.stopPropagation();
}
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, cfg.scrollDuration, 'swing', function () {
window.location.hash = target;
});
});
};
非常感谢您的帮助:)因为您在else语句中定义了
$target
,所以如果它没有进入else语句,它将是未定义的
您需要在else之外设置$target
if (target != "#top") {
// you do not set it here
...
else{
var target = this.hash, // <-- not sure why you are declaring target again....
$target = $(target); // you set it here
...
}
if(目标!=“顶部”){
//你不能把它放在这里
...
否则{
var target=this.hash,//试试这段代码,我认为这是可行的
var ssSmoothScroll = function() {
$(".smoothscroll").on("click", function(e) {
if (this.hash !== "" && this.pathname == window.location.pathname) {
var target = this.hash,
$target = $(target);
// $target = $(target);
e.preventDefault();
e.stopPropagation();
var topOffset = 0; //#top should default to 0 so no need to calculate the difference between top and top :)
if (target != "#top") {
//If the target is not "#top", then calculate topOffset
topOffset = $(target).offset().top;
}
$("html, body")
.stop()
.animate(
{
scrollTop: $target.offset().top
},
cfg.scrollDuration,
"swing",
function() {
window.location.hash = target;
}
);
}
});
};
我认为这
指的是事件参数