Toggle class to an element by click another element

前端 未结 5 2032
暗喜
暗喜 2021-01-16 09:52

I want to click on an element to toggle a class being referenced on a completely unrelated element (not a child, parent or sibling)

For example, initially the code w

5条回答
  •  無奈伤痛
    2021-01-16 10:16

    Try this:

    document.querySelector('div.navigation').classList.toggle('open');
    

    This will work if you only have one div element that has the class navigation. It would be better to give it an id, for example id=navigation

提交回复
热议问题