Toggle class to an element by click another element

前端 未结 5 2006
暗喜
暗喜 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:27

    You don't really need javascript. Checkboxes work great at storing on/off state. You just need to get a little crafty with the CSS to use it elsewhere. Here is an example:

    label.divcheck { color:blue; text-decoration:underline; }
    input.divcheck { display:none; }
    
    input.divcheck + div { display:none; }
    input.divcheck:checked + div { display:block;}
    
    
    
    
    
    
    
    

提交回复
热议问题