简体   繁体   English

jQuery如何刷新附加的div的特征?

[英]jQuery How to refresh appended div's characteristics?

It's a long story. 说来话长。 Please bear with me till the end. 请忍受我直到最后。 I have two parent divs #c1 and #c2 (there are actually 5, just for the sake of example). 我有两个父div#c1和#c2(仅出于示例目的,实际上有5个)。 Both have some child divs. 两者都有一些子div。 Each clild div has a delete div which on being clicked deletes that particular child div and the first child div from #c2 takes it's place (Just like a queue. Where a if person leaves, others behind him move forward). 每个clidiv div都有一个删除div,单击该删除div会删除该特定子div,然后从#c2的第一个子div代替它(就像一个队列。如果有人离开,后面的其他人会前进)。

HTML looks like this: HTML看起来像这样:

<div id="c1">
    <div class="a"><div class="delete"></div></div>
    <div class="b"><div class="delete"></div></div>
    <div class="c"><div class="delete"></div></div>
</div>

<div id="c2">
    <div class="d"><div class="delete"></div></div>
    <div class="e"><div class="delete"></div></div>
    <div class="f"><div class="delete"></div></div>
</div>

Now when 'a' or 'b' or 'c' is deleted then 'd', 'e' and 'f' move one place up. 现在,当删除“ a”,“ b”或“ c”时,则“ d”,“ e”和“ f”上移一位。 So now '#c1' has a,b and d and '#c2' has e and f. 因此,现在“#c1”具有a,b和d,而“#c2”具有e和f。

Here is my jquery code : 这是我的jQuery代码:

 $(".delete").click(function() {
 $(this).parent().remove();
 });    

$("#c1 .delete").click(function() {         
    if ($("#c1>div").length<3) {
        $("#c2>div:nth-child(1)").appendTo("#c1");
    }
});

This works perfectly fine untill I try to delete the moved up 'd'. 直到我尝试删除上移的“ d”,此方法才能正常工作。 When I try to delete 'd' it deletes but the child divs are not moving up. 当我尝试删除'd'时,它会删除,但子div不会向上移动。

Any help. 任何帮助。 Thanks for bearing with me all along. 感谢您一直以来的支持。

The problem is that $("#c1 .delete").click(...) is going to select the initial divs and attach the click handler to them; 问题是$("#c1 .delete").click(...)将选择初始div,并将点击处理程序附加到它们; it's not going to attach a click handler to any arbitrary div that will eventually be under #c1 . 不会将点击处理程序附加到最终将在#c1下的任何任意div上。 You can use event delegation to solve this: 您可以使用事件委托来解决此问题:

$('#c1').on('click', '.delete', function(e) {
    $(e.target).parent().remove();
    if ($("#c1>div").length < 3) {
        $("#c2>div:nth-child(1)").appendTo("#c1");
    }
});

This will just attach an event handler to #c1 that handles any clicks from descendant div.delete , even those that will be added later on. 这只会将事件处理程序附加到#c1 ,该事件处理程序将处理来自后代div.delete所有单击,甚至包括稍后添加的单击。

There are 2 errors in the following block of code 以下代码块中有2个错误

$("#c1 .delete").click(function() {         
    if (("#c1>div").length<3) {
        $("#c2>div:nth-child(1)").appendTo("#c1");
    }
]);

It should be like this: 应该是这样的:

$("#c1 .delete").click(function() {         
    if ($("#c1>div").length<3) {
        $("#c2>div:nth-child(1)").appendTo("#c1");
    }
});

The delete function will work on on the existing items. 删除功能将对现有项目起作用。 For all new itesm you should use on. 对于所有新项目,您都应使用。

So the code should be like this: 因此,代码应如下所示:

$('#c1').on('click', '.delete', function(e) {
    $(this).parent().remove();
    if ($("#c1>div").length<3) {
        $("#c2>div:nth-child(1)").appendTo("#c1");
    }
});

Also, you can combine both the code into just one block instead of using 2 different blocks. 另外,您可以将这两个代码仅合并为一个块,而不必使用2个不同的块。 I have created a sample jsfiddle with a working code. 我用工作代码创建了一个示例jsfiddle

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

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