Sometimes when we use a plugin such as jQuery UI, it will add a class and overwrites existig styles. But I want to make sure my own class is always applied to the element.
.red {color: red;}
.plugin_style {color: blue;}
<p id="foo" class="red">foo</p>
<a href="#" id="doSomething">do something</a>
$.fn.doSomething = function() {
$(this).css("border", "1px solid #f00").addClass("plugin_style");
return $(this);
};
$("#doSomething").on("click", function() {
// this adds class "plugin_style"
// generated source: class="red plugin_style"
$("#foo").doSomething();
// remove "red"
// generated source: class="plugin_style"
$("#foo").removeClass("red");
// add "red"
// generated source: class="plugin_style red"
$("#foo").addClass("red");
});
By removing and adding class red, red goes after every other classes in generated source, and should overwrite the style defined in plugin_style. But the color is still blue.
Can someone explain this?
update
By adding !important
may always keep .red applied. But without it, .red should also overwrites .plugin_style since it comes after.