JavaScript onClick addClass

后端 未结 3 1785
逝去的感伤
逝去的感伤 2020-12-16 16:13

I am kind of confused why my code doesn\'t work correctly, I hope You will tell me what I\'ve done wrong. I want to highlight navigation tab while clicked.

HTML:

3条回答
  •  借酒劲吻你
    2020-12-16 17:02

    This code will highlight the navigation tab without needing to include onclick in the HTML, though it doesn't remove the background color if another tab is clicked.

        document.onclick = function(event) {
             var target = event.target || event.srcElement;
             target.style.background = '#cf5c3f';
            };
    

    https://codepen.io/mdmcginn/pen/BwrNaj/

提交回复
热议问题