简体   繁体   English

如何在反应功能组件中使用 querySelectorAll() 添加或删除 class

[英]How can I add or remove class using querySelectorAll() in react functional component

I am trying to add or remove an active class if I click any list item but the code is not working.如果我单击任何列表项但代码不起作用,我将尝试添加或删除活动的 class。 Here is my function这是我的 function

    const list = document.querySelectorAll('.m-list') as NodeListOf<Element>

    function activeLink(this: any) {
        list.forEach((item) => item.classList.remove('active'))
        // list.forEach((item) => item.classList.add('active'))
        this.classList.add('active')
    }
    list.forEach(item => item.addEventListener('click', activeLink))

Full code with HTML完整代码 HTML



export default function MobileNavigation() {
    const list = document.querySelectorAll('.m-list') as NodeListOf<Element>

    function activeLink(this: any) {
        list.forEach((item) => item.classList.remove('active'))
        // list.forEach((item) => item.classList.add('active'))
        this.classList.add('active')
    }
    list.forEach(item => item.addEventListener('click', activeLink))



    return <div className='mobile-navigation'>
        <ul>
            <li className='m-list active'>
                <a href="#">
                    <span className='m-icon'><i className="fa-solid fa-house"></i></span>
                    <span className='m-text'>Home</span>
                </a>
            </li>
            <li className='m-list'>
                <a href="#">
                    <span className='m-icon'><i className="fa-solid fa-shop"></i></span>
                    <span className='m-text'>Shop</span></a>
            </li>
            <li className='m-list'>
                <a href="#"><span className='m-icon'><i className="fa-solid fa-store"></i></span>
                    <span className='m-text'>Vendors</span></a>
            </li>
            <li className='m-list'>
                <a href="#"><span className='m-icon'><i className="fa-solid fa-address-card"></i></span>
                    <span className='m-text'>About us</span></a>
            </li>
            <li className='m-list'>
                <a href="#"><span className='m-icon'><i className="fa-solid fa-headset"></i></span>
                    <span className='m-text'>Contact Us</span></a>
            </li>
            <div className="indicator"></div>
        </ul>
    </div>;
}

const list = document.querySelectorAll('.m-list') as NodeListOf<Element>

 function activeLink(e:any) {
        list.forEach((item) => item.classList.remove('active'))
        // list.forEach((item) => item.classList.add('active'))
        e.classList.add('active')
    }
 list.forEach(item => item.addEventListener('click', (e)=>activeLink(item)))

Just replace this with your code... it will start working now.只需用您的代码替换它……它现在就会开始工作。 I hope this helped you solve your issue:)我希望这可以帮助您解决问题:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用反应功能组件添加或删除 class - How can I add or remove class using react functional component 反应:如何访问 class 组件中的功能组件的道具 - React: How can I acces to props of a functional component in a class component 我怎样才能把这个 React class 组件变成一个功能组件? - How can i turn this React class component into a functional component? 我如何编写此类组件来响应功能组件。? - How can i write this class component to react functional component.? 如何使用 React Hooks 将此级联下拉 class 组件转换为功能组件? - How can I convert the this cascading dropdown class component to functional one using React Hooks? 单击按钮时,如何添加选定的 class 并从 React 功能组件中的所有其他按钮中删除? - On button click, how to add selected class and remove from all other buttons in React functional component? 如何编写返回 class 组件作为功能组件的 React 组件? - How can I write React component that returns a class component as a functional component? 如何反应才能识别它是 class 组件还是功能组件? - How react can Identify it's a class component or functional component? 如何在 React Native 中将此功能组件更改为基于类的组件? - How can I change this functional component into a class based component in react native? 如何将此 Class 组件 function 转换为 React Native 中的功能组件 - How can I convert this Class component function into functional component in React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM