gpt4 book ai didi

javascript - 单击按钮打开模态窗口时出现无法读取属性 'top'

转载 作者:行者123 更新时间:2023-11-29 15:35:31 25 4
gpt4 key购买 nike

以下代码在页面加载时起作用。但是,当我单击应该打开模式窗口的按钮时,我收到以下错误:

未捕获的类型错误:无法读取未定义的属性“top”

我该如何修复这个错误?

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);
});

最佳答案

您收到错误的可能原因是,$(hash) 元素对于您的模态按钮不存在。而模态按钮是属于$(".page-scroll a[href^='#'], #intro a").on('click')事件的元素。如果没有 id 等于单击按钮的“href”属性的元素,则无法获得它的“offset.top”。放入“console.log(hash)”来检查你得到了什么。

可能的解决方案:

if ($(hash).length) {
$('html, body').stop().animate({
scrollTop: $(hash).offset().top - 100}, 2000, 'easeOutExpo');
}

关于javascript - 单击按钮打开模态窗口时出现无法读取属性 'top',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29729196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com