简体   繁体   English

如何使用JQuery从样式表中删除CSS属性?

[英]How to remove CSS attribute from a stylesheet using JQuery?

I am trying to figure out how to remove css attributes using Jquery. 我试图弄清楚如何使用Jquery删除CSS属性。 The issue is that it seems like this can only be done if a style is inline. 问题是,似乎只有在样式内联时才能执行此操作。 For instance, when I use this approach: 例如,当我使用这种方法时:

('.hero').css('background-image', '').css('background-color', ''); 

It does nothing, although if it has those attributes set using the STYLE property, than it works great. 它什么都不做,虽然它有使用STYLE属性设置的那些属性,但它工作得很好。 The same can be said for: 同样可以说:

('.hero').css('background-image', 'none').css('background-color', 'transparent');

Can anyone assist me in removing attributes that are added via stylesheet and not inline? 任何人都可以帮助我删除通过样式表而不是内联添加的属性吗?

You can do this, but it is very convoluted. 您可以执行此操作,但这非常麻烦。 The only way to accomplish this is to load the stylesheet into a string in javascript, grep out the rules you want to remove (using String.replace() or similar), then remove the current css tag from the page (using $.remove()), and then adding a new css tag with the grepped contents of the file/text you loaded. 实现此目的的唯一方法是将样式表加载到javascript中的字符串中,grep out您要删除的规则(使用String.replace()或类似的东西),然后从页面中删除当前的css标记(使用$ .remove ()),然后添加一个新的css标记,其中包含您加载的文件/文本的grepped内容。

This is very very convoluted. 这非常令人费解。 I think you need to rethink why you are trying to do this to begin with. 我想你需要重新考虑为什么要开始这样做。 Or maybe just stick with setting the values back to their defaults using jQuery, which can be found on w3schools . 或者只是坚持使用jQuery将值设置回默认值,这可以在w3schools上找到。 Or maybe create a style in the stylesheet that sets the values to their defaults, and give the element that style. 或者可以在样式表中创建一个样式,将值设置为默认值,并为该样式赋予元素。 OR just give us a little more info, and we may be able to suggest a better way around your problem. 或者只是给我们更多信息,我们也许可以为您解决问题提供更好的方法。

I think you may be asking the wrong question. 我认为您可能会问错问题。

It looks like you want to be able to restyle an element without removing its existing class. 看起来您希望能够在不删除现有类的情况下重新生成元素。 A far easier way to do this is to ADD an additional class to the item you want differently styled, and then handle it in the CSS definition. 更简单的方法是为想要不同样式的项添加一个额外的类,然后在CSS定义中处理它。

For instance: 例如:

('.hero').addClass("blank");

with CSS: 使用CSS:

.hero.blank { background-color: transparent; }

As .hero.blank is more specific than .hero , it'll be the style applied first. 由于.hero.blank.hero更具体,因此它将首先应用于样式。

You have to set a default style for elements. 您必须为元素设置默认样式。 Few default CSS properties from W3C . W3C几乎没有默认的CSS属性 Most of default properties are listed here . 此处列出了大多数默认属性。

it looks like to me you're trying to adjust several css attributes in the same function. 在我看来,您正在尝试在同一函数中调整多个CSS属性。

if you want to clear the background image and make the background transparent then you just need to change both in the same .css function 如果要清除背景图像并使背景透明,则只需在同一.css函数中进行更改

try 尝试

$('.hero').css("background-image:none; background-color:transparent");

or just set up a 2nd css class that has the style preformatted "heroAlt" and use jQuery to remove old class/add new class 或只设置样式已预先格式化为“ heroAlt”的第二个CSS类,并使用jQuery删除旧类/添加新类

$('.hero').removeClass(function(){
    $(this).addClass("heroAlt");
});

hope that helps 希望有所帮助

First of all, the following will work if used correctly: 首先,如果使用正确,以下内容将起作用:

$('.hero').css('background-image', 'none').css('background-color', 'transparent');

See: http://jsfiddle.net/Az7VZ/ 请参阅: http//jsfiddle.net/Az7VZ/

That being said, there are a few pitfalls to watch out for. 话虽这么说,但要注意一些陷阱。

  1. You run your JavaScript before the HTML document containing the .hero div loads. 在包含.hero div的HTML文档加载之前运行JavaScript。 This has the effect of jQuery selecting nothing to apply the CSS to. 这有jQuery选择什么来应用CSS的效果。

  2. Your CSS uses the "!important" modifier. 您的CSS使用“!important”修饰符。 You will not be able to overload that style with jQuery. 您将无法使用jQuery重载该样式。

  3. Also, don't forget the "$" or "jQuery" in your script. 另外,不要忘记脚本中的“$”或“jQuery”。

Note: this DOES NOT override the actual .hero class. 注意:这不会覆盖实际的.hero类。 Therefore, if you add another element with the "hero" class, then it will have the original CSS styling. 因此,如果您使用“hero”类添加另一个元素,那么它将具有原始的CSS样式。 You would need to run the jQuery script again to apply the new style to the new element. 您将需要再次运行jQuery脚本,以将新样式应用于新元素。

A great alternative to this, is creating different classes with the desired styles. 一个很好的替代方案是创建具有所需样式的不同类。 Then removing/adding the CSS class via jQuery: 然后通过jQuery删除/添加CSS类:

$('.hero').removeClass('hero').addClass('hero2');

See: http://jsfiddle.net/Az7VZ/1/ 请参阅: http//jsfiddle.net/Az7VZ/1/

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

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