简体   繁体   English

检查 div 的显示 CSS

[英]Check div's display CSS

How can I do something like this?我怎么能做这样的事情?

Example I have 2 divs:示例我有 2 个 div:

I wanna do something like this... If 'some-class' display=none;我想做这样的事情......如果'some-class' display=none; then 'some-other-class' display=none;然后 'some-other-class' display=none;

What do I need to use to do something like this jQuery, javascript?我需要用什么来做这样的 jQuery、javascript? Thank you.谢谢你。

Can you provide a code snippet or more clarity?你能提供一个代码片段或更清楚吗?

With jQuery, you can use:使用 jQuery,您可以使用:

$('.element').is(':visible') 
   ? $('.other-class').hide() 
   : $('.other-class').show()

Or with vanillaJS或者使用 vanillaJS

element.style.display === 'block' 
  ? other_element.style.display = 'block'
  : other_element.style.display = 'none'

Or even better way (I think)甚至更好的方法(我认为)

other_element.style.display = element.style.display

The last way take the element style display value and assign to other_element style display.最后一种方式取element样式显示值并赋值给other_element样式显示。

Here is a method taking advantage of css variables.这是一种利用 css 变量的方法。 I set a variable named --class-display on the root element, and then I can use that variable in any place throughout my css.我在根元素上设置了一个名为--class-display的变量,然后我可以在整个 css 的任何地方使用该变量。 When I want to hide that element, I can use js to change the root variable property, and it will apply throughout my css.当我想隐藏该元素时,我可以使用 js 来更改根变量属性,它将应用于我的整个 css。

 document.querySelector(".someClass").addEventListener("click", async () => { const root = document.documentElement.style; root.setProperty("--class-display", "none"); await new Promise(r => setTimeout(r, 1000)); root.setProperty("--class-display", "block"); });
 :root { --class-display: block; } body { display: flex; justify-content: space-around; } div { height: 4rem; aspect-ratio: 1; background: black; color: white; }.someClass, .otherClass { display: var(--class-display); }
 <div class="someClass"><b>CLICK ME</b></div> <div class="otherClass"></div>

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

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