关闭并将高度和宽度重置为“0”上的原始尺寸;关闭“;点击

close and reset height and width to its original sizes on "close" click

本文关键字:点击 关闭 原始 高度      更新时间:2023-09-26

我有这个html:

<div class="box">
     <div class="info">
          <p>..some content..</p>
          <a href="#" class="close">Close</a>
     </div>
</div>
<div class="box">
     <div class="info">
          <p>..some content..</p>
          <a href="#" class="close">Close</a>
     </div>
</div>

由于我不知道.box的高度,因为它会扩展,它的高度将取决于.info内容,我运行这个:

 $('.box').each(function() {
     $(this).data('height', $(this).height());
 });

当我单击"关闭"一次.box展开时,我应该能够关闭展开的.box,并将其高度和宽度重置为展开前的原始大小。

我试着运行这个,它在另一个函数中,但没有得到正确的高度:

$(".close").click(function (e) {
    e.preventDefault();
    $(".info").empty();
    $(".box").width(251);
    $(".box").data('height');
});

有人吗?

关于你的.close方法:

$(".close").click(function (e) {
    e.preventDefault();
    $(".info").empty();
    $(".box").width(251);
    $(".box").data('height'); // Just retrieves the height value
});

我想你想这么做?

$(".close").click(function (e) {
    e.preventDefault();
    $(".info").empty();
    $(".box").width(251);
    // Sets the element's height to the stored height value
    $(".box").height( $(".box").data('height') );
});