简体   繁体   English

使用复选框更改 css 样式

[英]Change a css style using a checkbox

I'm making an hamburger menu for my website.我正在为我的网站制作汉堡菜单。 When I click on this hamburger, I want the rest of my page to be dark.当我点击这个汉堡包时,我希望我页面的 rest 变暗。

To achieve this, I added a div:为此,我添加了一个 div:

<div id="black"></div>

My HTML:我的HTML:

<nav role="navigation">
    <div id="menuToggle">
      <input type="checkbox" id="mycheckbox"/>
      <span></span>
      <span></span>
      <span></span>
      <?php
        wp_nav_menu(
          array(
            'theme_location' => 'top_menu',
            'container' => 'ul', // afin d'éviter d'avoir une div autour
            'menu_class' => 'site__header__menu', // ma classe personnalisée
          )
        );
      ?>
    </div>
  </nav>
  <div id="black"></div>

This div is in display: none, and all i want is that when i click on my hamburger menu, the display style switchs to inline/block.这个 div 显示:无,我想要的是当我单击我的汉堡菜单时,显示样式切换为内联/块。

I tried with JavaScript:我试过 JavaScript:

function functionTest() {
const cb = document.querySelector('#mycheckbox');
const black = document.querySelector('#black');
  if (cb.checked) {
    black.style["display"] = "inline";
  } else {
    black.style["display"] = "none";
  }
}

It doesn't work.它不起作用。 Thanks in advance提前致谢

You can achieve this using pure CSS using the tidial selector.您可以使用潮汐选择器使用纯 CSS 来实现此目的。

#mycheckbox:checked ~ div #black {
   display:block;
}

 body {background:#fff;} #black { background: #ccc; display:none; padding:20px; position:absolute; left:0;right:0;top:30px;bottom:0;}.opened-menu #black {display:block;}.item, .menu { display:inline-block; min-width:100px; font-size:14px; border:1px solid; padding:10px; text-align:center; } #mycheckbox:checked ~ div #black { display:block; }
 <input type="checkbox" id="mycheckbox"/> <label for="mycheckbox"> Menu </label> <div> <div id="black"> <a href="#" class="item">Item</a> <a href="#" class="item">Item</a> <a href="#" class=" menu">Menu</a> </div> </div>

If you want to do it CSS only, you can do the following.如果你只想做CSS,你可以这样做。 Keep in mind it does not work on Firefox or older browsers check compatibility here .请记住,它不适用于 Firefox 或更旧的浏览器,请在此处检查兼容性

#black {
    display: none;
}

nav:has(cb:checked) + #black {
    display: inline;
}

Working in action: https://jsfiddle.net/6vwmy95L/22/在行动中工作: https://jsfiddle.net/6vwmy95L/22/

Otherwise, you can use an event listener;否则,您可以使用事件侦听器;

const checkbox = document.querySelector("#mycheckbox");
const black = document.querySelector('#black');

if (checkbox) {
  checkbox.addEventListener("change", (event) => {
    if (event.currentTarget.checked && black) {
      black.style.display = "inline";
    } else {
      black.style.display = "none";
    }
  });
}

As @somethinghere said, if you are able to change your HTML to have a better structure and have greater browser support go for it.正如@somethinghere 所说,如果您能够更改 HTML 以获得更好的结构并获得更好的浏览器支持 go。

If you are unable to change your HTML for some reason or another and do not need to support older browsers, you can use the:has approach.如果您出于某种原因无法更改 HTML 并且不需要支持旧版浏览器,则可以使用:has 方法。

If you are unable to change your HTML and you need to support older browsers, use the JavaScript approach.如果您无法更改您的 HTML 并且您需要支持较旧的浏览器,请使用 JavaScript 方法。

You need the site to work even when JavaScript is disabled and support older browsers, then refactor your HTML and use @Marin HTML 's answer.即使 JavaScript 被禁用并支持旧版浏览器,您也需要该网站正常工作,然后重构您的 HTML 并使用@Marin HTML 的答案。

Thanks to everyone for your help.感谢大家的帮助。 I just changed this:我只是改变了这个:

<input type="checkbox" id="mycheckbox" onclick="functionTest()"/>

Jan Pfeifer said: "There is no event handler attached to anything. For example ", this solved my problem ! Jan Pfeifer 说:“没有任何事件处理程序附加到任何东西。例如”,这解决了我的问题!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM