Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript]Usuwanie pozostałych divów
degistivepl
post 8.07.2018, 21:16:37
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
trueblue
post 8.07.2018, 21:30:31
Post #2





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
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 8.07.2018, 21:40:43
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. });
questionmark.gifquestionmark.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
trueblue
post 8.07.2018, 21:47:55
Post #4





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

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


Przepisz choć poprawnie podany kod.


--------------------
Go to the top of the page
+Quote Post
degistivepl
post 8.07.2018, 21:55:56
Post #5





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

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


Cytat(trueblue @ 8.07.2018, 22:47:55 ) *
Przepisz choć poprawnie podany kod.

Jak niby inaczej ma to być
Go to the top of the page
+Quote Post
trueblue
post 8.07.2018, 22:04:19
Post #6





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

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


Tak jak podałem.


--------------------
Go to the top of the page
+Quote Post
degistivepl
post 9.07.2018, 00:32:21
Post #7





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

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


Cytat(trueblue @ 8.07.2018, 23:04:19 ) *
Tak jak podałem.

A jak usunąć? Bo o to na początku pytałem.
Go to the top of the page
+Quote Post
trueblue
post 9.07.2018, 05:31:12
Post #8





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

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


Przecież użyłeś już metody removeChild.


--------------------
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 18.04.2024 - 17:28