Skocz do zawartości

Recommended Posts

Napisano (edytowany)

Cześć,

Mam problem z przechodzeniem po elementach tablicy. Stworzyłem tablicę 'pages', której dodałem jakieś elementy i kiedy chciałem przejść po niej pętlą for pisząc ten kod (jak coś wszystko co wstawiłem do tablicy już wcześniej zadeklarowałem):



const pages = [papierniaSmolec, papierniaSmolec2, papierniaSmolec3, papierniaSmolec4, papierniaSmolec5, papierniaSmolec6, papierniaSmolec7, papierniaSmolec8, papierniaSmolec9];


let pair = 2;

        for (let i = 2; i <= pages.length; i++) {
            if (pages[i].pairOfElements == `pair${pair}`) {
                const page = pages[i - 1].createIndexPage();
                const page2 = pages[i].createIndexPage();

                page.style.marginRight = '25vw';
                page2.style.display = 'flex';

            }
        }
        pair++;

to w konsoli przeglądarki wyskoczył mi ten błąd:

b300f5e0919385307578d95791f5347a.png.30b3a38f6676bf7e71f81b5ebfa92dc3.png

 

Wiem, że chodzi tutaj to to, że pages[i] nie jest zdefiniowane ale nie wiem dlaczego ten błąd mi wyskakuje skoro mam stworzoną tą tablicę i dodane do niej 9 elementów.

Jak coś każdy element tej tablicy jest obiektem tego typu:

function page(backgroundLinkImage, projectLink, feedbackLink, className, pairOfElements, className2 = "", className3 = "", className4 = "") {
    this.backgroundLinkImage = backgroundLinkImage;
    this.projectLink = projectLink;
    this.reviewLink = feedbackLink;
    this.className = className;
    this.className2 = className2;
    this.className3 = className3;
    this.className4 = className4;
    this.pairOfElements = pairOfElements;

	this.createIndexPage = function() {

        let indexLiElement = document.createElement('li');
        indexLiElement.setAttribute('class', `portfolio-start__elements portfolio-start__elements--pages ${className} ${className2} 			${className3} ${className4} ${pairOfElements}'`);

        let indexLinkElement = document.createElement('a');
        indexLinkElement.setAttribute('href', `${projectLink}`)
        indexLinkElement.setAttribute('class', 'portfolio-start__link');

        let indexSpanElement = document.createElement('span');
        indexSpanElement.setAttribute('class', 'portfolio-start__text')
        indexSpanElement.innerText = 'Zobacz Projekt';

        let indexImgElement = document.createElement('img');
        indexImgElement.setAttribute('src', 'img/project_icon.png');
        indexImgElement.setAttribute('class', 'portfolio-start__img2');

        indexLinkElement.appendChild(indexImgElement);
        indexLinkElement.appendChild(indexSpanElement);


        let indexLinkElement2 = document.createElement('a');
        indexLinkElement2.setAttribute('href', `${feedbackLink}`)
        indexLinkElement2.setAttribute('class', 'portfolio-start__link');

        let indexSpanElement2 = document.createElement('span');
        indexSpanElement2.setAttribute('class', 'portfolio-start__text')
        indexSpanElement2.innerText = 'Zobacz Opinię';

        let indexImgElement2 = document.createElement('img');
        indexImgElement2.setAttribute('src', 'img/review-icon.png');
        indexImgElement2.setAttribute('class', 'portfolio-start__img2');

        indexLinkElement2.appendChild(indexImgElement2);
        indexLinkElement2.appendChild(indexSpanElement2);

        indexLiElement.appendChild(indexLinkElement);
        indexLiElement.appendChild(indexLinkElement2);

        indexLiElement.style.backgroundImage = "url(" + backgroundLinkImage + ")";
        indexLiElement.style.background = 'cover';

        var windowWidth = document.body.offsetWidth;

        if (pairOfElements != 'pair1' && windowWidth <= 688)
            indexLiElement.style.display = 'none';

        return indexLiElement;
    }
}

Pozdrawiam.

Edytowano przez Piotrek

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gość
Odpowiedz...

×   Wkleiłeś zawartość bez formatowania.   Usuń formatowanie

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Utwórz nowe...