a菜单点击标红,其他标黑代码

   <script>
        let aList = document.querySelectorAll('a');
        console.log(aList);
        for (let index = 0; index < aList.length; index++) {
            const element = aList[index];
            element.addEventListener("click", function (e) {
                let txt = e.srcElement.innerText;
                element.style.color = "red";//点击则标红
                for (let index = 0; index < aList.length; index++) {
                    const item = aList[index];
                    if (item.innerText !== txt) {
                        item.style.color = "black";//其他标黑
                    }
                }
            });
        }
    </script>
<a href='#'>首页</a>
<a href='#'>关于</a>

posted on 2022-08-17 14:16  anjun_xf  阅读(19)  评论(0编辑  收藏  举报

导航

TOP