javascript RemoveChild只删除nodelist的最后一个元素,但不删除第一个元素

jjhzyzn0  于 2023-04-10  发布在  Java
关注(0)|答案(2)|浏览(181)

你好,我在javaScript上开发滑块。首先,我在init上创建3张卡片,然后单击右箭头,我正在创建下3张卡片,以前的卡片需要删除。所以我使用removeChild(slider.childNodes[i])hovewer它只删除我添加的新卡片,留下旧的,我也尝试了slider.removeChild(slider.firstChild和lastChild)bu它没有帮助eather。下面是代码

import { pets } from '/js/pets-list.js';

const arrowLeft = document.getElementById('arrow-left');
const arrowRight = document.getElementById('arrow-right');
const mobArrowLeft = document.getElementById('mobile-arrow-left');
const mobArrowRight = document.getElementById('mobile-arrow-right');

const slider = document.getElementById('slider');

let sliderPositionLeft = slider.style.left;

for (let i = 0; i < 3; i++) {
    let newCardR = document.createElement('div');
    newCardR.className = 'slider-content_card';

    let newCardImg = document.createElement('img');
    newCardImg.src = 'assets/modal-images/woody.png';
    newCardImg.alt = 'slider-pet-image';

    let newCardText = document.createElement('p');
    newCardText.textContent = 'Woody';

    let newCardButton = document.createElement('button');
    newCardButton.className = 'button_secondary';
    newCardButton.textContent = 'Learn more';

    newCardR.appendChild(newCardImg);
    newCardR.appendChild(newCardText);
    newCardR.appendChild(newCardButton);

    slider.appendChild(newCardR);
}

let clickCounterR = 0;

arrowRight.addEventListener('click', () => {
    clickCounterR++;
    console.log(slider.childNodes);
    for (let j = 0; j < 3; j++) {
        debugger;
        let newCardR = document.createElement('div');
        newCardR.className = 'slider-content_card';

        let newCardImg = document.createElement('img');
        newCardImg.src = 'assets/modal-images/woody.png';
        newCardImg.alt = 'slider-pet-image';

        let newCardText = document.createElement('p');
        newCardText.textContent = 'Woody';

        let newCardButton = document.createElement('button');
        newCardButton.className = 'button_secondary';
        newCardButton.textContent = 'Learn more';

        newCardR.appendChild(newCardImg);
        newCardR.appendChild(newCardText);
        newCardR.appendChild(newCardButton);

        slider.appendChild(newCardR);
    }
    if (clickCounterR === 2) {
        debugger;
        for (let i = 0; i < 3; i++) {
            slider.removeChild(slider.firstChild);
        }
        clickCounterR = 0;
    }
    sliderPositionLeft = sliderPositionLeft - 1004;
    slider.style.left = sliderPositionLeft + 'px';
});
sqserrrh

sqserrrh1#

使用

elementToRemove.remove();

使用element.remove()而不是parent.removeChild(element),您不必担心在父元素上应用方法或使用正确的索引。

nkcskrwz

nkcskrwz2#

由于你的代码首先删除新元素,你应该尝试先删除旧的卡片,然后 * 再 * 添加新的卡片(所以,切换你的两个for循环):

arrowRight.addEventListener('click', () => {
    clickCounterR++;
    console.log(slider.childNodes);
    if (clickCounterR === 2) {
        debugger;
        for (let i = 0; i < 3; i++) {
            slider.removeChild(slider.firstChild);
        }
        clickCounterR = 0;
    }
    for (let j = 0; j < 3; j++) {
        debugger;
        let newCardR = document.createElement('div');
        newCardR.className = 'slider-content_card';

        let newCardImg = document.createElement('img');
        newCardImg.src = 'assets/modal-images/woody.png';
        newCardImg.alt = 'slider-pet-image';

        let newCardText = document.createElement('p');
        newCardText.textContent = 'Woody';

        let newCardButton = document.createElement('button');
        newCardButton.className = 'button_secondary';
        newCardButton.textContent = 'Learn more';

        newCardR.appendChild(newCardImg);
        newCardR.appendChild(newCardText);
        newCardR.appendChild(newCardButton);

        slider.appendChild(newCardR);
    }
    sliderPositionLeft = sliderPositionLeft - 1004;
    slider.style.left = sliderPositionLeft + 'px';
});

相关问题