我怎么能做這樣的事情?
示例我有 2 個 div:
我想做這樣的事情......如果'some-class' display=none; 然后 'some-other-class' display=none;
我需要用什么來做這樣的 jQuery、javascript?謝謝你。
uj5u.com熱心網友回復:
使用 jQuery,您可以使用:
$('.element').is(':visible')
? $('.other-class').hide()
: $('.other-class').show()
或者使用 vanillaJS
element.style.display === 'block'
? other_element.style.display = 'block'
: other_element.style.display = 'none'
或者更好的方式(我認為)
other_element.style.display = element.style.display
最后一種方式取element樣式顯示值并分配給other_element樣式顯示。
uj5u.com熱心網友回復:
這是一種利用 css 變數的方法。我設定了一個以--class-display根元素命名的變數,然后我可以在整個 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/359031.html
標籤:javascript html 查询 css
下一篇:二叉搜索樹遍歷-查找最近值
