2017-05-25 23 views
5

Chcę sklonować zawartość elementu div przy pomocy jQuery, ale z skopiowanej zawartości chcę usunąć klasę z oryginalnej przed użyciem funkcji appendTo. Kiedy usuwam klasę z klonu, usuwają ją również z oryginalnej.Klonuj i usuń klasę bez zmiany oryginalnej.

Mój kod to:

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

}); 

Należy pamiętać, że nie chcę, aby usunąć klasę od rzeczywistej div skąd skopiowano zawartość, po prostu chcę, aby usunąć go z skopiowany kod tak, że jest nie zawarte w klonowanym dziale.

+0

nie widzę kod, aby usunąć klasę, można zaktualizować swój kod z klasy usuwania, w którym starasz się usunąć klasa – LazyDeveloper

+0

next.children („: first-child”). RemoveClass ("koledzy sm-offset-1 '). clone(). appendTo ($ (this)); // tutaj usuwam klonową klasę div – suraj

Odpowiedz

0

można usunąć elementu ze sklonowanego obiektu, a następnie sklonować do nowego obiektu, który byłby coś jak poniżej:

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

    }); 

Wymień „swój-element tożsamości klasy” z nazwą klasy chcesz usuwać. Oryginalny ref. od - How to remove the selected element during a clone() operation

0

Powinieneś być w stanie uruchomić removeClass() na obiekcie przed jego dołączeniem, niezależnie od tego, gdzie chcesz go dołączyć.

+0

next.children (': first-child'). removeClass ('col-sm-offset-1') .clone(). appendTo ($ (this)) ; Używam tego, ale jego remopve z oryginalnego div również – suraj

3

Możesz użyć następujących opcji: removeClass i addClass po clone.

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

@Suraj usuń klasę po klonie. –

+0

dzięki tonu sir – suraj

+0

Jeśli ans rozwiązać problem, proszę go zaakceptować. –

Powiązane problemy