Javascript image slider tutorial: "TypeError: slides is null"

Javascript image slider tutorial: "TypeError: slides is null"

我按照教程制作了一个图像滑块,并将所有内容复制到 T(或者我认为),但在教程中它在这一点上起作用了——因为我的什么也没做。

开发者工具调试器标记错误“TypeError: slides is null”,参考第 10 行、第 4 行和第 3 行。

我已经三重检查我没有打错任何字,并且完全按照教程中的内容复制出来,但仍然无法正常工作。

我是 Javacsript 的新手,我需要在一个项目中使用类似这样的东西,但我已经不知所措,所以如果这是一个愚蠢的问题,我深表歉意。非常感谢任何帮助。

const buttons = document.querySelectorAll("[data-carousel-button]")

buttons.forEach(button => {
    button.addEventListener("click", () => {
        const offset = button.dataset.carouselButton === "next" ? 1 : -1
        const slides = button
            .closest("[data-carousel]")
            .querySelector("data-slides")

        const activeSlide = slides.querySelector("[data-active]")
        let newIndex = [...slides.children].indexOf(activeSlide) + offset
        if (newIndex < 0) newIndex = slides.children.length - 1
        if (newIndex >=  slides.children.length) newIndex = 0

        slides.children[newIndex].dataset.active = true
        delete activeSlide.dataset.active
    })
})
* *::before, *::after {
    padding:0;
    margin:0;
    box-sizing: border-box;
}

.carousesl {
    width: 100vw;
    height: 100vh;
    position: relative;
}

.slide {
    position: absolute;
    inset: 0;
    opacity: 0;
}

.slide > img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit:cover;
    object-position: center;

}

.slide[data-active] {
    opacity:1;
}

.carousel-button {
    position: absolute;
    background: none;
    border: none;
    font-size: 4rem;
    top: 50%;
    transform: translateY(-50%);
    z-index: 2;
    color: rgba(255, 255, 255, .5);
    cursor: pointer;
    border-radius: .25rem;
    padding: 0.5rem;
    background-color: rgba(0, 0, 0, .1);
}

.carousel-button:hover,
.carousel-button:focus {
    color: white;
    background-color: rgba(0, 0, 0, .2);
}

.carousel-button.next {
    left: 1rem;
}
.carousel-button.prev {
    right: 1rem;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JS Image Slider</title>
    <link rel="stylesheet" href="styles.css">
    <script src="script.js" defer></script>
</head>
<body>
    <section aria-label="Project Photos">
        <div class="carousel" data-carousel>
            <button class="carousel-button prev" data-carousel-button="prev">&#x1F86A;</button>
            <button class="carousel-button next" data-carousel-button="next">&#x1F868;</button>
            <ul data-slides>
                <li class="slide" data-active>
                    <img src="https://images.unsplash.com/photo-1653629154029-265d18f0e1f5?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1740&q=80" alt="Nature image #1">
                </li>
                <li class="slide">
                    <img src="https://images.unsplash.com/photo-1629447236132-22c57cd0f0bf?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1932&q=80" alt="Nature image #2">
                </li>
                <li class="slide">
                    <img src="https://images.unsplash.com/photo-1653161926463-725f4b39a739?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1742&q=80" alt="Nature image #3">
                </li>
                <li class="slide">
                    <img src="https://images.unsplash.com/photo-1653422064161-a2e82924adbc?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1932&q=80" alt="Nature image #4">
                </li>
                <li class="slide">
                    <img src="https://images.unsplash.com/photo-1629447236132-22c57cd0f0bf?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1932&q=80" alt="Nature image #5">
                </li>
            </ul>
        </div>
    </section>
</body>
</html>

是你忘记加括号了

而不是:

const slides =
button.closest("[data-carousel]").querySelector("data-slides")

使用:

const slides = button.closest("[data-carousel]").querySelector("[data-slides]")