关闭并在“关闭”点击时将高度和宽度重置为原始大小

时间:2012-02-16 00:32:26

标签: javascript jquery

我有这个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');
});

任何?

1 个答案:

答案 0 :(得分:1)

关于.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') );
});