gpt4 book ai didi

javascript - 在 Jquery load() 之后设置 div 的高度

转载 作者:行者123 更新时间:2023-11-28 10:10:20 26 4
gpt4 key购买 nike

我有一个使用 Jquery 将内容加载到 div 中的函数。我正在尝试添加到我的功能中,能够设置 div 的高度以适应我正在加载的内容。我已经尝试了 height() 函数的几种变体,并且我查看了其他人的代码,但我不认为我真的理解如何将它合并到我的特定代码中。这是功能。它还有一些代码来更新 URL。 contentarea 是加载所有内容的 div 和需要调整大小的 div。

    //Jquery loader
function getHash() {
return window.location.hash
}

$("a").on("click", function (e) {
var page = this.href.replace("#", "") + ".html",
hash = $(this).prop("hash");

$('#contentarea').load(page, function () {
if (page.match("home.html")) {
history.pushState('', document.title, window.location.pathname);
}
else {
location.hash = hash;
}
});

e.preventDefault();
});'

任何帮助将不胜感激!

更新:我在您的帮助下更新了代码,但它并没有真正起作用。我想我会在 contentarea 中添加一个名为 content 的 div,并使用回调函数。我做错了什么?

'

$('#contentarea').load(page, function () {
if (page.match("home.html")) {
history.pushState('', document.title, window.location.pathname);
} else {
location.hash = hash;
}
}, function () {
$('#contentarea').height($('#content').height());
});

'

最佳答案

.load() 的文档在函数完成时提供回调

JQuery doc

$('#contentarea').load(page, function(){},function(){})

第二个函数在函数加载完成时被调用。

关于javascript - 在 Jquery load() 之后设置 div 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24413224/

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