Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [JavaScript]Usuwanie pozostałych divów
degistivepl
post
Post #1





Grupa: Zarejestrowani
Postów: 13
Pomógł: 0
Dołączył: 5.04.2018

Ostrzeżenie: (0%)
-----


Cześć, zrobiłem coś takiego i 3 pierwsze divy w divie orders-container są przenoszone na koniec a z góry usuwane, a jak zrobić żeby zostały te 3 divy a reszta się usunęła?
  1. var orderscontainer = document.getElementById("orders-container");
  2.  
  3. orderscontainer.addEventListener("click", Show);
  4. elements = orderscontainer.children;
  5. var newelements = [];
  6.  
  7. function Show() {
  8. for (var i=0; i<elements.length; i++)
  9. {
  10. if (i < 3)
  11. {
  12. newelements.push(elements[i]);
  13. }
  14. }
  15. for (var i=0; i<newelements.length; i++) {
  16. orderscontainer.appendChild(newelements[i]);
  17. }
  18. }
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
trueblue
post
Post #2





Grupa: Zarejestrowani
Postów: 6 807
Pomógł: 1828
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


Przenosisz na koniec i pozostałe chcesz usunąć? To jaki ma sens w ogóle przenoszenie?
Jeśli chcesz usunąć wszystkie <div> poza pierwszymi trzema, to usuń elementy na podstawie selektora: n+4

  1. [].forEach.call(orderscontainer.querySelectorAll('div:nth-child(n+4)'), function(el){
  2. //tu usuń
  3. });
Go to the top of the page
+Quote Post
degistivepl
post
Post #3





Grupa: Zarejestrowani
Postów: 13
Pomógł: 0
Dołączył: 5.04.2018

Ostrzeżenie: (0%)
-----


Cytat(trueblue @ 8.07.2018, 22:30:31 ) *
Przenosisz na koniec i pozostałe chcesz usunąć? To jaki ma sens w ogóle przenoszenie?
Jeśli chcesz usunąć wszystkie <div> poza pierwszymi trzema, to usuń elementy na podstawie selektora: n+4

  1. [].forEach.call(orderscontainer.querySelectorAll('div:nth-child(n+4)'), function(el){
  2. //tu usuń
  3. });

  1. elements.forEach.call(orderscontainer.querySelectorAll('div:nth-child(n+4)'), function(el){
  2. orderscontainer.removeChild(el);
  3. });
(IMG:style_emoticons/default/questionmark.gif) (IMG:style_emoticons/default/questionmark.gif) Nie można pobrać właściwości „call” dla niezdefiniowanego lub pustego odwołania

Ten post edytował degistivepl 8.07.2018, 21:41:14
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.10.2025 - 12:28