2017-05-25 108 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

如果解決您的問題,請接受它。 –