【问题标题】:How to change the height of a dynamic div using jquery如何使用jquery改变动态div的高度
【发布时间】:2023-01-27 00:27:26
【问题描述】:

我有一个名为 .spacer 的 div,它动态地增加了一个高度。我现在想更改 .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();
});

【问题讨论】:

  • 您需要在代码中添加一个有效的 sn-p

标签: jquery


【解决方案1】:

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

您 sn-p 更正:

 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","")) 行中的内容)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2011-09-06
    • 2010-11-29
    • 1970-01-01
    • 2012-06-19
    • 2012-01-13
    • 1970-01-01
    相关资源
    最近更新 更多