0

我的演示非常清楚,当我克隆我的元素时,我克隆并删除了 div,但是 :last 元素 (li) 不应该被删除。我错过了什么?

与此有关!:

$(document).on("click", 'li.delete',function () {
            $(this).closest(".outerDiv").remove();
    if ($(this).is(".outerDiv:last")){
            return false;
    }

演示:http: //jsfiddle.net/XeELs/86/

JQUERY var cloneCount = 0; $("#add-address").click(function() {

    $("#to-add-address").clone()
        .attr("id", "to-add-address_Clone" + cloneCount)
        .insertAfter("#to-add-address");
    $("#clone", "#to-add-address_Clone" + cloneCount)
        .attr("id", "clone_Clone" + cloneCount);
    cloneCount++;
});

$(document).on("click", '.options li a',function () {
            $(this).closest(".options").find('li a').removeClass('selected');
            $(this).addClass('selected');

        });
$(document).on("click", 'li.delete',function () {
            $(this).closest(".outerDiv").remove();
    if ($(this).is(".outerDiv:last")){
            return false;
    }

});

4

2 回答 2

1

使用size()方法找出有多少地址块,这将让您删除它们直到只剩下一个:

$(document).on("click", 'li.delete',function () {

    if ($('.outerDiv').size() > 1){
            $(this).closest(".outerDiv").remove();
    }

}

http://jsfiddle.net/2mby5/

于 2012-04-13T16:29:24.660 回答
1

试试这个代码

var cloneCount = 0;
$("#add-address").click(function() {

    $("#to-add-address").clone()
        .attr("id", "to-add-address_Clone" + cloneCount)
        .insertAfter("#to-add-address").addClass('cloned');  //add a new class cloned to the cloned outerDivs
    $("#clone", "#to-add-address_Clone" + cloneCount)
        .attr("id", "clone_Clone" + cloneCount);
    cloneCount++;
});

$(document).on("click", '.options li a',function () {
            $(this).closest(".options").find('li a').removeClass('selected');
            $(this).addClass('selected');

        });
$(document).on("click", 'li.delete',function () {
            $(this).closest(".outerDiv").filter('.cloned').remove(); // and delete only the cloned ones
    if ($(this).is(".outerDiv:last")){
            return false;
    }   
});
于 2012-04-13T16:33:17.927 回答