【发布时间】:2016-06-10 12:27:43
【问题描述】:
对css中类的优先级感到困惑,我知道最后添加的类具有最高优先级,但在这里 就我而言,我认为我错了,我将类动态添加到 div,但类无法正常工作
<p>Click the button to add the "mystyle" class to DIV.</p>
<script>
function myFunction() {
document.getElementById("myDIV").classList.add("mystyle","hello");
}
</script>
<style>
.hello{
background-color: blue;
}
.mystyle {
background-color: red;
}
</style>
为什么 div 显示红色而不是蓝色?
【问题讨论】:
-
你回答你自己的问题:
class added at the **last** has **highest priority**- 你的最后一节课是.myStyle,所以... -
我的意思是在使用这样的类名时说
<p class="mystyle hello">@somethinghere -
是的,我意识到阅读下面@kevinto 的答案! HTML 中的类声明与它们的重要性无关,或者在添加它们时,它们都是基于 CSS 中的规则而不是 HTML 中的规则进行评估的。
-
天啊!在 HTML 中,所有类都是相同的 = 它将使用 CSS 来确定更重要的。
标签: javascript html css