克隆div并删除类而不更改原始类

sur*_*raj 5 javascript jquery twitter-bootstrap bootstrap-modal

我想使用jQuery克隆div的内容,但是在我使用appendTo函数之前,我想从复制的内容中删除该类.当我从克隆中删除该类时,它们也会从原始类中删除.

我的代码是:

$('.carousel .item').each(function(){
        var next = $(this).next();

        if (!next.length) {
            next = $(this).siblings(':first');
        }
        next.children(':first-child').clone().appendTo($(this));
        next.children(':first-child').addClass('col-sm-offset-1');

        for (var i=0;i<3;i++) {
            next=next.next();
            if (!next.length) {
                next = $(this).siblings(':first');
            }
            next.children(':first-child').clone().appendTo($(this));                 
        }

});
Run Code Online (Sandbox Code Playgroud)

请注意,我不想从复制内容的实际div中删除类,我只是想从复制的代码中删除它,以便它不包含在克隆的div中.

小智 1

您可以先从克隆对象中删除元素,然后克隆到新对象,如下所示:

$('.carousel .item').each(function(){ var next = $(this).next();    
        if (!next.length) {
            next = $(this).siblings(':first');
        }
        var $block = next.children(':first-child');

        // Grab the and remove element class
        $block = $block.find('.your-element-identity-class').removeClass('your-element-identity-class');

        // Clone the block 
        var $clone = $block.clone();

        $clone.appendTo($(this));
        next.children(':first-child').addClass('col-sm-offset-1');

        for (var i=0;i<3;i++) {
            next=next.next();
            if (!next.length) {
                next = $(this).siblings(':first');
            }
            var $block = next.children(':first-child');

            // Grab the and remove element class
            $block = $block.find('.your-element-identity-class').removeClass('your-element-identity-class');

            // Clone the block 
            var $clone = $block.clone();

            $clone.appendTo($(this));                
        }

    });
Run Code Online (Sandbox Code Playgroud)

将“your-element-identity-class”替换为您要删除的类名。原始参考号 from -如何在克隆()操作期间删除选定的元素