Javascript 无法读取属性';顶部';在单击按钮打开模式窗口时发生
以下代码在页面加载时起作用。但是,当我单击一个按钮以打开一个模式窗口时,我得到以下错误: 未捕获的TypeError:无法读取未定义的属性“top” 如何修复此错误Javascript 无法读取属性';顶部';在单击按钮打开模式窗口时发生,javascript,jquery,bootstrap-modal,Javascript,Jquery,Bootstrap Modal,以下代码在页面加载时起作用。但是,当我单击一个按钮以打开一个模式窗口时,我得到以下错误: 未捕获的TypeError:无法读取未定义的属性“top” 如何修复此错误 if ($(window).width() > 992) { (function($) { "use strict"; $(".page-scroll a[href^='#'], #intro a").on('click', function(e) { e.pr
if ($(window).width() > 992) {
(function($) {
"use strict";
$(".page-scroll a[href^='#'], #intro a").on('click', function(e) {
e.preventDefault();
var hash = this.hash;
$('html, body').stop().animate({
scrollTop: $(hash).offset().top - 100}, 2000, 'easeOutExpo');
});
})(jQuery);
$('body').scrollspy({
target: '.navbar',
offset: 110
});
// alert("large");
}
else {
(function($) {
"use strict";
$(".page-scroll a[href^='#'], #intro a").on('click', function(e) {
e.preventDefault();
var hash = this.hash;
$('html, body').stop().animate({
scrollTop: $(hash).offset().top - 50}, 2000, 'easeOutExpo');
});
})(jQuery);
$('body').scrollspy({
target: '.navbar',
offset: 70
});
// alert("small");
}
//modal popup function
function popup_modal(item){
var link = $(item).attr('id');
$('#bootstrap_modal').load('/'+link+'');
$('#bootstrap_modal').modal('show');
$("#bootstrap_modal").on("show", function () {
$("body").addClass("modal-open");
}).on("hidden", function () {
$("body").removeClass("modal-open")
});
}
//Modal pop up
$('.mmodal').on('click', function(){
popup_modal(this);
});
请尝试以下代码:
if ($(window).width() > 992) {
(function($) {
"use strict";
$(".page-scroll a[href^='#'], #intro a").on('click', function(e) {
e.preventDefault();
var hash = this.hash;
var newVal = $(hash).offset().top - 100;
$('html, body').stop().animate({
scrollTop: newVal}, 2000, 'easeOutExpo');
});
})(jQuery);
$('body').scrollspy({
target: '.navbar',
offset: 110
});
// alert("large");
}
else {
(function($) {
"use strict";
$(".page-scroll a[href^='#'], #intro a").on('click', function(e) {
e.preventDefault();
var hash = this.hash;
var newVal = $(hash).offset().top - 50;
$('html, body').stop().animate({
scrollTop: newVal}, 2000, 'easeOutExpo');
});
})(jQuery);
$('body').scrollspy({
target: '.navbar',
offset: 70
});
// alert("small");
}
//modal popup function
function popup_modal(item){
var link = $(item).attr('id');
$('#bootstrap_modal').load('/'+link+'');
$('#bootstrap_modal').modal('show');
$("#bootstrap_modal").on("show", function () {
$("body").addClass("modal-open");
}).on("hidden", function () {
$("body").removeClass("modal-open")
});
}
//Modal pop up
$('.mmodal').on('click', function(){
popup_modal(this);
});
我已经将变量
newVal
的值设置为$(hash).offset()。top-50
您得到错误的可能原因是,对于模式按钮的情况,$(hash)
元素不存在。模式按钮是一个元素,它属于$(“.页面滚动a[href^=''.'.''.',#简介a”)。在('click')
事件中。如果没有id等于单击按钮的“href”属性的元素,则无法获取其“offset.top”。
放入“console.log(hash)
”检查您在那里得到了什么
一种可能的解决办法:
if ($(hash).length) {
$('html, body').stop().animate({
scrollTop: $(hash).offset().top - 100}, 2000, 'easeOutExpo');
}
看到html也很好,它应该与$(hash.offset().top-50},2000,'easoutexpo')有关;我试过了,但它产生了一个奇怪的错误“意外的标记”)“我键入的内容与上面显示的完全一样,我犯了一个语法错误,我的错。现在看看,它不应该返回任何错误。此外,如果您的示例使用JSFIDLE或类似的语言,那就太好了。在不知道发生了什么的情况下进行调试是很困难的。工作起来很有魅力。谢谢你说。