简体   繁体   English

覆盖 CSS Class 如何?

[英]Overwriting a CSS Class How to?

I have a Soft.CSS file which has following class我有一个 Soft.CSS 文件,它具有以下 class

.check_out {
width:58px;
background-image:url(/a/i/softadd/checkout.gif);}

What i want to do here is change this Class ( please note that i DO NOT have access to Soft.CSS as it is fed from some other server ) to我在这里要做的是更改此 Class (请注意,我无权访问 Soft.CSS,因为它是从其他服务器提供的)

.check_out {
width:58px;
background-image:url(/myserver/checkout.gif);}

How do i do it?我该怎么做? using CSS?使用 CSS? Using Jquery?使用 Jquery?

I think it would be enough if you make sure that your piece of css comes AFTER the Soft.css file.我认为如果你确保你的 css 出现在Soft.css文件之后就足够了。 Most browsers only pick the last definition they find when they find double entries of the same identification.大多数浏览器仅在找到相同标识的双重条目时才选择他们找到的最后一个定义。

Elsewise, you could parse the Soft.css file with PHP and do some regex on background-image and find the file name.否则,您可以使用 PHP 解析Soft.css文件,并对background-image执行一些正则表达式并找到文件名。

UPDATE更新
An alternative answer:另一个答案:

Have you thought of adding another class on every element that has the check_out -class?您是否考虑过在每个具有check_out类的元素上添加另一个 class ? You could write your own css on some other class like check_out_ow and add it with jQuery:您可以在其他一些 class (如check_out_ow ,并将其与 jQuery 一起添加:

$(document).ready(function()
{
    // every element gets an extra class with custom information
    $('check_out').addClass('check_out_ow');
});

I'm not sure, but I hope that the information of two classes lies deeper than of one class.我不确定,但我希望两个类的信息比一个 class 更深。 Your css could look like:您的 css 可能如下所示:

.check_out.check_out_ow {
width:58px;
background-image:url(/myserver/checkout.gif);}

If you want to redefine exactly the rule itself, not the respective css properties, well, you do have such an opportunity in javascript (not jquery)如果您想重新定义规则本身,而不是相应的 css 属性,那么您在 javascript(不是 jquery)中确实有这样的机会

There is document.styleSheets collection we can access.我们可以访问 document.styleSheets 集合。 Each item is CSSStyleSheet, which has cssRules collection.每个项目都是 CSSStyleSheet,其中有 cssRules 集合。 But Schroedingers Cat is right, there are rare occasions when we really need to access rules directly.但是薛定谔猫是对的,在极少数情况下我们真的需要直接访问规则。

Check this classic reference if you want mess up with DOM CSS.如果您想弄乱 DOM CSS,请查看经典参考。

But in your case it will definitely be better, as already metioned, to add a separate stylesheet with redefined rules.但是在您的情况下,正如已经提到的那样,添加一个带有重新定义规则的单独样式表肯定会更好。

As mentioned already, you only need to add your rule, either in another CSS file or directly to the page in <style> tags, after you include Soft.css.如前所述,您只需要在另一个 CSS 文件中或在<style>标记中直接添加您的规则,在您包含 Soft.css 之后。

If the order in which your CSS files are attached is also out of your control you can use increased specificity to ensure that your rule is seen as more important:如果您的 CSS 文件的附加顺序也超出您的控制范围,您可以使用增加的特异性来确保您的规则被视为更重要:

div.check_out { /* assuming the element is a div */
width:58px;
background-image:url(/myserver/checkout.gif);}

a simple solution is to give your style a higher specificity, no matter it was loaded after soft.CSS or even it was loaded before , all you have to do is to make your CSS selector for .check_out have a higher specificity, by just adding any parent class (or even ID will be better), so lets say you have a <div id="wrapper"> , that wraps all elements, then you can override your style as:一个简单的解决方案是为您的样式提供更高的特异性,无论它是在soft.CSS之后加载还是之前加载,您所要做的就是让您的.check_out选择器具有更高的特异性,只需添加任何父 class (甚至 ID 会更好),所以假设你有一个<div id="wrapper"> ,它包装了所有元素,然后你可以覆盖你的风格:

#wrapper .check_out {
    width:58px;
    background-image:url(/myserver/checkout.gif);
}

also you can add this id to the body (for example) if you have access to that..如果您有权访问该 ID,您也可以将此 ID 添加到正文中(例如)。

refer to the following article , to know more about specificity things参考下面的文章,了解更多关于特异性的东西

$(function () {
    $('.check_out').css('background-image', '/myserver/checkout.gif');
});

jsFiddle example jsFiddle 示例

It will update all instances of .check_out in the output page.它将更新 output 页面中的所有.check_out实例。 I assume this is exactly what you want.我认为这正是你想要的。 You don't want to edit the class in the stylesheet itself since you don't have access to it.您不想在样式表本身中编辑 class,因为您无权访问它。

A jquery approach would be to select on the class - $(".check_out") - and amend the css ( which I can't remember how to do ). A jquery approach would be to select on the class - $(".check_out") - and amend the css ( which I can't remember how to do ).

It does not change the class, but will change everything using the class.它不会更改 class,但会使用 class 更改所有内容。 Of course, if you have to have the class changed because other elements are added using it, then this might not work.当然,如果您必须更改 class 因为使用它添加了其他元素,那么这可能不起作用。

It depends very much on your precise requirements and solution.这在很大程度上取决于您的精确要求和解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM