首页 > 解决方案 > 在不应该出现的地方出现错误

问题描述

我不知道为什么我被要求在句号应该放置逗号的地方放置逗号。const 卡是说当这是设置变量的方法时我不能在那里设置它。

我试图从 const 更改为 let 并重组 .forEach 语法

class TabLink {
    constructor(tabElement) {
        // assign this.tabElement to the tabElement DOM reference
        this.tabElement = tabElement;

        // Get the `data-tab` value from this.tabElement and store it here
        this.tabData = this.tabElement.dataset.tab;

        // We need to find out if a user clicked 'all' cards or a specific category.  Follow the instructions below to accomplish this task:    

        // Check to see if this.tabData is equal to 'all'
        if (this.tabElement.dataset.tab === "all") {
            // If `all` is true, select all cards regardless of their data attribute values
            this.cards = document.querySelectorAll(".card");
        } else {
            // else if `all` is false, only select the cards with matching this.tabData values
            this.cards = document.querySelectorAll(`.card[tabData="${this.tabData}"]`);
        }

        // Map over the newly converted NodeList we just created in our if statement above. Convert each this.cards element into a new instance of the TabCard class. Pass in a card object to the TabCard class. 
        this.cards = Array.from(this.cards).map((card) => new TabCard(card));


        // Add a click event that invokes this.selectTab
        this.tabElement.addEventListener('click', () => { this.selectTab() });
    }

    selectTab() {

            // Select all elements with the .tab class on them
            // const tabs = document.querySelectorAll();
            const tabs = document.querySelectorAll('.tab');

            // Iterate through the NodeList removing the .active-tab class from each element
            Array.from(tabs).forEach((tabs))
            link.classList.remove(".active-tab");
        }
        // 

    // Select all of the elements with the .card class on them
    const cards = document.querySelectorAll('.card');

    // Iterate through the NodeList setting the display style each one to 'none'
    cards.forEach(card => {
        card.style.display = "none";
    })

    // Add a class of ".active-tab" to this.tabElement
    this.tabElement = ".active-tab";

    // Notice we are looping through the this.cards array and invoking selectCard() from the TabCard class. Just un-comment the code and study what is happening here.
    this.cards.forEach(card => card.selectCard());
    console.log(this.cards);
}

'const' 只能在 .ts 文件中使用。';' 预期的。',' 预期的。',' 预期的。意外的标记。需要构造函数、方法、访问器或属性。预期声明或声明。

标签: javascriptarraysjquery-selectorselement

解决方案


推荐阅读