-
Notifications
You must be signed in to change notification settings - Fork 172
feat: DOM Elements practise #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 5 commits
85aa16c
1b0e0da
6e7f59f
d440a71
485da0d
a66b708
e016568
f242cc2
cb90bd7
6de7c0a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,18 @@ | ||
| console.log('DOM'); | ||
| const commentsItemNewest = document.querySelector('.comments__item', '.comments__item--newest'); | ||
| const childrenOfLi = commentsItemNewest.querySelectorAll('p'); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TUtaj już mamy obiekt "tablicopodobny" czyli mamy dostęp do length więc zamiast pętli wystarczy po prostu: |
||
|
|
||
| let listOfSpan; | ||
| const spanArr = []; | ||
|
|
||
| const listOfP = childrenOfLi.forEach((el) => { | ||
| listOfSpan = el.querySelectorAll('span'); | ||
|
|
||
| for (let i = 0; i < listOfSpan.length; i++) { | ||
| spanArr.push(listOfSpan[i]); | ||
| } | ||
| }); | ||
|
|
||
| console.log( | ||
| 'Zostało znalezionych elementów posiadających atrybut data-info w liczbie:', | ||
| spanArr.length | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,44 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="pl"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge"> | ||
| <meta name="author" content="Mateusz Bogolubow"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <meta http-equiv="X-UA-Compatible" content="ie=edge" /> | ||
| <meta name="author" content="Mateusz Bogolubow" /> | ||
| <title>devmentor.pl - JS DOM Elements - #01</title> | ||
| </head> | ||
| <body> | ||
| </head> | ||
| <body> | ||
| <ul class="comments"> | ||
| <li class="comments__item comments__item--newest"> | ||
| <p>Lorem ipsum <span data-info="https://devmentor.pl">dolor</span> sit amet consectetur adipisicing elit. Iure, vero! <span data-info="https://devmentor.pl">Lorem</span> ipsum dolor sit amet, consectetur adipisicing elit. Nobis, amet?</p> | ||
| <p>Lorem ipsum dolor sit amet consectetur, <span data-info="https://devmentor.pl">adipisicing</span> elit. Magnam temporibus necessitatibus repellendus! Earum fugit distinctio facere iusto nesciunt! Voluptatem, perspiciatis?</p> | ||
| </li> | ||
| <li class class="comments__item"> | ||
| <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. <span data-info="https://devmentor.pl">Iure</span>, vero! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nobis, amet?</p> | ||
| <p>Lorem ipsum dolor <span data-info="https://devmentor.pl">sit</span> amet consectetur, adipisicing elit. Magnam temporibus necessitatibus repellendus! Earum fugit distinctio facere iusto nesciunt! Voluptatem, perspiciatis?</p> | ||
| </li> | ||
| <li class="comments__item comments__item--newest"> | ||
| <p> | ||
| Lorem ipsum | ||
| <span data-info="https://devmentor.pl">dolor</span> | ||
| sit amet consectetur adipisicing elit. Iure, vero! | ||
| <span data-info="https://devmentor.pl">Lorem</span> | ||
| ipsum dolor sit amet, consectetur adipisicing elit. Nobis, amet? | ||
| </p> | ||
| <p> | ||
| Lorem ipsum dolor sit amet consectetur, | ||
| <span data-info="https://devmentor.pl">adipisicing</span> | ||
| elit. Magnam temporibus necessitatibus repellendus! Earum fugit distinctio facere iusto | ||
| nesciunt! Voluptatem, perspiciatis? | ||
| </p> | ||
| </li> | ||
| <li class class="comments__item"> | ||
| <p> | ||
| Lorem ipsum dolor sit amet consectetur adipisicing elit. | ||
| <span data-info="https://devmentor.pl">Iure</span> | ||
| , vero! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Nobis, amet? | ||
| </p> | ||
| <p> | ||
| Lorem ipsum dolor | ||
| <span data-info="https://devmentor.pl">sit</span> | ||
| amet consectetur, adipisicing elit. Magnam temporibus necessitatibus repellendus! Earum | ||
| fugit distinctio facere iusto nesciunt! Voluptatem, perspiciatis? | ||
| </p> | ||
| </li> | ||
| </ul> | ||
|
|
||
| <script src="./app.js"></script> | ||
| </body> | ||
| </html> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,11 @@ | ||
| console.log('DOM'); | ||
| const aList = document.querySelectorAll('a'); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Można też od razu wyszukać odpowiednie przez |
||
| console.log(aList); | ||
|
|
||
| let hasAttribute; | ||
|
|
||
| const hasLink = aList.forEach((link) => { | ||
| if (link.hasAttribute('data-url')) { | ||
| hasAttribute = link.getAttribute('data-url'); | ||
| link.setAttribute('href', hasAttribute); | ||
| } | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,45 @@ | ||
| console.log('DOM'); | ||
|
|
||
| const buttonSettings = { | ||
| attr: { | ||
| className: 'btn', | ||
| title: 'super button' | ||
| }, | ||
| css: { | ||
| border: '1px solid #336699', | ||
| padding: '5px 20px', | ||
| color: '#444' | ||
| }, | ||
| text: 'Click me!', | ||
| } | ||
| attr: { | ||
| className: 'btn', | ||
| title: 'super button', | ||
| }, | ||
| css: { | ||
| border: '1px solid #336699', | ||
| padding: '5px 20px', | ||
| color: '#444', | ||
| }, | ||
| text: 'Click me!', | ||
| }; | ||
|
|
||
| const button = document.createElement('button'); | ||
|
|
||
| for (const key in buttonSettings) { | ||
| if (key === 'attr') { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Jeśli robimy if-y na każdy przypadek to nie będzie czytelniej zrobić to oddzielnie? tj. Teraz widać, żę dla |
||
| for (const key in buttonSettings.attr) { | ||
| if (key === 'className') { | ||
| button.classList.add(key); | ||
| } else { | ||
| button.setAttribute(key, buttonSettings.attr[key]); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| key === 'text' ? (button.textContent = buttonSettings.text) : undefined; | ||
|
|
||
| if (key === 'css') { | ||
| for (const key in buttonSettings.css) { | ||
| if (key === 'border') { | ||
| button.style[key] = buttonSettings.css[key]; | ||
| } else if (key === 'padding') { | ||
| button.style[key] = buttonSettings.css[key]; | ||
| } else if (key === 'color') { | ||
| button.style[key] = buttonSettings.css[key]; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const sectionEl = document.querySelector('section'); | ||
| sectionEl.appendChild(button); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,27 @@ console.log('DOM'); | |
|
|
||
| // struktura do wykorzystania w pętli | ||
| const menuItems = [ | ||
| {text: 'start', url: '/'}, | ||
| {text: 'galeria', url: '/gallery'}, | ||
| {text: 'kontakt', url: '/contact'}, | ||
| ]; | ||
| { text: 'start', url: '/' }, | ||
| { text: 'galeria', url: '/gallery' }, | ||
| { text: 'kontakt', url: '/contact' }, | ||
| ]; | ||
|
|
||
| const navEl = document.querySelector('nav'); | ||
|
|
||
| const ulEl = document.createElement('ul'); | ||
| navEl.appendChild(ulEl); | ||
|
|
||
| const arr = []; | ||
|
|
||
| const createLinkList = menuItems.forEach((item) => { | ||
| const liStartEl = document.createElement('li'); | ||
| ulEl.appendChild(liStartEl); | ||
|
|
||
| arr.push(liStartEl); | ||
|
|
||
| const addAEl = document.createElement('a'); | ||
| liStartEl.appendChild(addAEl); | ||
|
|
||
| addAEl.setAttribute('href', item.url); | ||
| addAEl.textContent = item.text; | ||
| }); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,35 @@ | ||
| console.log('DOM'); | ||
|
|
||
| const curr = document.querySelector('.js-curr'); | ||
| let curr = document.querySelector('.js-curr'); | ||
| const currParent = curr.parentElement; | ||
|
|
||
| const currParentSibling = currParent.nextElementSibling; | ||
| currParentSibling.setAttribute('title', 'nextSiblingElement'); | ||
|
|
||
| const currLastParentSibling = currParentSibling.nextElementSibling; | ||
| console.log(currLastParentSibling); | ||
| const newParagraph = document.createElement('p'); | ||
|
|
||
| const cloneArt = currParent.cloneNode(true); | ||
| const art = document.querySelector('.articles'); | ||
|
|
||
| art.insertAdjacentElement('afterbegin', cloneArt); | ||
|
|
||
| newParagraph.textContent = 'Lorem Ipsum dolor sit amet'; | ||
| const pEl = currLastParentSibling.querySelector('p'); | ||
| currLastParentSibling.insertBefore(newParagraph, pEl); | ||
|
|
||
| const btn = document.createElement('button'); | ||
| btn.textContent = 'Usuń z koszyka'; | ||
| curr.insertAdjacentElement('afterend', btn); | ||
|
|
||
| let siblingEl = curr.nextElementSibling; | ||
| const siblingsArr = []; | ||
|
|
||
| while ((curr = curr.nextElementSibling)) { | ||
| siblingsArr.push(siblingEl); | ||
| } | ||
|
|
||
| siblingsArr.forEach((el) => { | ||
| el.className = '.siblings'; | ||
| }); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Co jeśli |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Czy faktycznie został wyszukany zadany element czy po prostu pierwszy o klasie
.comments__item? (efekt ten sam, ale treść zadania była inna)Tutaj selektor powinien wyglądać tak:
querySelector('.comments__item.comments__item--newest');- teraz zadziała jak należy :)