jQuery undo具有多个remove();

时间:2018-08-08 05:47:58

标签: javascript jquery

如何获取始终记住上一个removed .items的顺序的函数,以便我可以undoundo timeout发生之前删除的项目一样多?

我希望能够快速remove全部.items,然后按undo来替换所有三个,一个接一个地按撤消按钮替换最后删除的项。

目前,我只能替换最后删除的.item

var undo = false;
var remove;
var timeout;
$(document).ready(function() {
  /*DELETE*/
  $('body').on('click', '.fa-times', function() {
    if ($('.item').hasClass("temp_deleted")) {
      $('.item.temp_deleted').remove();
    }
    remove = $(this).parent().parent();
    var undo_time = 10000;
    remove.animate({
      height: "0px"
    }, 200, function() {
      $(this).addClass('temp_deleted').hide();
    });
    function_undo(remove, undo);
    //undo
    $('.undo').addClass('active');
    clearTimeout(timeout);
    timeout = setTimeout(function() {
      $('.undo').removeClass('active');
      if (undo === false) {
        $('.item.temp_deleted').remove();
      }
    }, undo_time);

  });
  /*UNDO*/
  $('.undo div').click(function() {
    undo = true;
    function_undo(remove, undo);
    $(this).parent().removeClass('active');
  });
});

function function_undo(remove, undo) {
  if (undo == true) {
    remove.css('height', 'auto');
    remove.show();
    remove.removeClass('temp_deleted');
  }
}
.item {
  width: 100px;
  height: 50px;
  border: 2px solid
}

.actions span.fa-times:hover {
  color: #fe4444;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="list">
  <div class="item">
    <div class="actions">
      <span class="fa fa-times"></span>
    </div>
  </div>
  <div class="item">
    <div class="actions">
      <span class="fa fa-times"></span>
    </div>
  </div>
  <div class="item">
    <div class="actions">
      <span class="fa fa-times"></span>
    </div>
  </div>
</div>
</div>
<div class="undo">
  <div>
    <span class="fa fa-undo"></span> Undo
  </div>
</div>

1 个答案:

答案 0 :(得分:2)

正如我所说,您可以将它们保存在一个数组中。当您删除它时,请push将其放入数组中。当您要撤消某些操作时,pop退出。

顺便说一句,就像@LexJacobs所说的,不要remove。只是hide

不确定这是否是您想要的。但我正在尝试将其结构化。

var undo = false;
var timeout;
var arr = [];
$(document).ready(function() {
  /*DELETE*/
  $('body').on('click', '.fa-times', function() {
    if ($('.item').hasClass("temp_deleted")) {
      $('.item.temp_deleted').hide();
    }
    remove = $(this).parent().parent();
    var undo_time = 10000;
    remove.animate({
      height: "0px"
    }, 200, function() {
      $(this).addClass('temp_deleted').hide();
    });
    function_undo(remove, undo);
    //undo
    $('.undo').addClass('active');
    clearTimeout(timeout);
    timeout = setTimeout(function() {
      $('.undo').removeClass('active');
      if (undo === false) {
        $('.item.temp_deleted').hide();
      }
    }, undo_time);
    arr.push(remove);

  });
  /*UNDO*/
  $('.undo div').click(function() {
    undo = true;
    var remove = arr.pop();
    function_undo(remove, undo);
    $(this).parent().removeClass('active');
  });
});

function function_undo(remove, undo) {
  if (undo == true) {
    remove.css('height', 'auto');
    remove.show();
    remove.removeClass('temp_deleted');
  }
}
.item {
  width: 100px;
  height: 50px;
  border: 2px solid
}

.actions span.fa-times:hover {
  color: #fe4444;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="list">
  <div class="item">
    <div class="actions">
      <span class="fa fa-times"></span> 1
    </div>
  </div>
  <div class="item">
    <div class="actions">
      <span class="fa fa-times"></span> 2
    </div>
  </div>
  <div class="item">
    <div class="actions">
      <span class="fa fa-times"></span> 3
    </div>
  </div>
</div>
</div>
<div class="undo">
  <div>
    <span class="fa fa-undo"></span> Undo
  </div>
</div>