2017-05-25 106 views
5

我想克隆div的内容使用jQuery,但从复制的内容我想从原来的一个类删除我使用appendTo函数之前。当我从克隆中删除课程时,他们也从原始课程中删除课程。克隆div和删除类没有改变原来的

我的代码是:

$('.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));     
     } 

}); 

请注意,我不想从实际DIV从那里我复制了内容删除类,我只是想从复制的代码中删除,以便它是不包括在克隆的div中。

+0

我没有看到代码删除类,您可以更新删除类,你在哪里试图删除类 – LazyDeveloper

+0

next.children代码(“:第一个孩子”)。removeClass(“同事SM-偏移-1' )克隆)。()appendTo($(本); //这里我删除克隆div类 – suraj

回答

0

您可以从克隆的对象先删除元素,然后复制到新的对象,它会像如下所示:

$('.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));     
     } 

    }); 

替换“你的元素,身份类”与你希望你的类名去除。 原始文献。 from - How to remove the selected element during a clone() operation

0

在追加对象之前,您应该能够在对象上运行removeClass(),无论您想将其追加到哪里。

+0

next.children(':first-child')。removeClass('col-sm-offset-1').clone()。appendTo($(this)) ;我使用这个,但它从原始div的remopve也 – suraj

3

您可以在clone之后使用removeClassaddClass这样。

.clone().removeClass('oldClass').addClass('col-sm-offset-1') 
+0

@Suraj克隆后删除类。 –

+0

感谢口气先生 – suraj

+0

如果解决您的问题,请接受它。 –