且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JQuery中的addClass是否会覆盖任何现有的基于CSS类的样式?

更新时间:2023-09-14 14:04:52

您可以根据

You can override other classes using addClass according to the rules of CSS specificity just as CSS classes override/inherit properties from each other when using the class attribute on the element.

您的第二个示例起作用的原因是,因为它等效于在元素上设置style属性,根据CSS的特定性,该属性几乎是最特定的.

The reason your second example works, is because it is equivalent to setting the style attribute on the element which is pretty much the most specific according to CSS specificity.

相反,第一个示例不起作用,因为可能有一个CSS类比.dibHighlight

Conversely, the first example doesn't work because there is probably a CSS class that is more specific then .dibHighlight