如何使用jquery更改动态div的高度

tquggr8v  于 2023-01-30  发布在  jQuery
关注(0)|答案(1)|浏览(125)

我有一个动态添加高度的div,名为.spacer,现在我想改变.spacer的高度,将40px添加到当前动态高度,当切换时,将在动态.spacer高度上添加和删除40px。

<div class="spacer"></div> -- Shows an empty div which is then populated with a dynamic height.
let spacer = $(".spacer");

 $(".profile-header-wrapper .search-icon").on("click", function(){
     fixedVideo.toggleClass("fixed-video-position");
     spacer.css("height", (spacer + 50) + "px").toggle();
});
mzaanser

mzaanser1#

您要将高度设置为spacer + 50,而spacer实际上是一个jQuery元素,您应该使用spacer.height() + 50
您更正了摘录:

let spacer = $(".spacer");

 $(".profile-header-wrapper .search-icon").on("click", function(){
     fixedVideo.toggleClass("fixed-video-position");
     spacer.css("height", (spacer.height() + 50) + "px").toggle();
});

现在我假设您使用“toggle”,因为元素是隐藏的,在隐藏的元素上,spacer.height()可能返回空,您必须从CSS中获取它(使用parseInt(spacer.css("height").replace("px",""))行中的内容)。

相关问题