【发布时间】:2022-01-12 19:11:26
【问题描述】:
如何向具有相同 class 的 2 个 div 添加一个类?
在 this example 我有 3 个div,其中 2 个共享 class ".matched"。
如何在 JS 中向这 2 个 div 添加另一个类(例如 .sameclass)?
提前非常感谢!
<div class="container">
<div class="box1 matched"></div>
<div class="box2 matched"></div>
<div class="box3"></div>
</div>
CSS
.container {
display: flex;
}
.box1 {
width: 100px;
height: 100px;
background-color: red;
}
.box2 {
width: 100px;
height: 100px;
background: green;
}
.box3 {
width: 100px;
height: 100px;
background: purple;
}
【问题讨论】:
-
也许这会对你有所帮助:stackoverflow.com/questions/32586594/…
-
当您不知道哪个 CSS 类是由 2 个元素共享的时候,您是在问这个问题吗?或者您是否已经知道它应该是“匹配的”?
标签: javascript html css