简体   繁体   English

jQuery:删除元素后如何序列化ID

[英]jQuery : How can I serialize id after removing an element

I am dragging the elements and incrementing its id I just want to know how can I serialize the id`s after removing elements. 我正在拖动元素并增加其ID,我只想知道删除元素后如何序列化ID。

$(document).on('click', '.js-remove', function(event) {
        event.preventDefault();
        /* Act on the event */

        $(this).slideDown();
            $(this).animate({
                "opacity" : "0",
                },{
                "complete" : function() {
                    $(this).parent().remove();
                }
            });

    });

UPDATE : 更新:

  1. In here the serialize means ( if suppose I have dragged the elements three times and the id test-clone-1, test-clone-2 and test-clone-3 respectively to each div and now if I remove the second div with id test-clone-2 after removing that the third div with id test-clone-3 becomes test-clone-2 在这里,序列化方式(如果假设我已经将元素拖了三遍,并将id test-clone-1,test-clone-2和test-clone-3分别拖到每个div,现在如果我将第二个div与id test一起删除, -clone-2,删除ID为test-clone-3的第三个div成为test-clone-2
  2. To See the id increment please open the console 要查看ID增量,请打开控制台

jsFiddle link jsFiddle链接

You can Re-numbering Id's test-clone-X by adding this loop to "complete" function: 您可以通过将以下循环添加到"complete"功能中来对ID的test-clone-X重新编号:

//Re-numbering id's of each div inside .test class.
$('.test').each(function(i, obj) {
    $(this).attr("id", "test-clone-" + ( i + 1 ) );
});

See Updated Fiddle 参见更新的小提琴

Inspect element and see the new id's when you removing any element. 检查元素,并在删除任何元素时查看新的ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM