如何使用jQuery取消设置元素的CSS属性? [英] How do I unset an element's CSS attribute using jQuery?

查看:209
本文介绍了如何使用jQuery取消设置元素的CSS属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用以下方式在特定元素上设置CSS值:

  $('#element')。css background-color','#ccc'); 

我想要能够取消设置该元素特定的值并使用级联值of:

  $('#element')。css('background-color',null); 

但是这种语法似乎无效 - 使用另一种语法>

提前感谢!



编辑:值不会从父元素继承 - 原始值来自元素级选择器。非常抱歉,任何混淆!

解决方案

jQuery docs


将style属性的值设置为空字符串 - 例如 $('#mydiv')。css('color','') - 如果元素已经被直接应用,属性,通过jQuery的 .css()方法,或通过直接DOM操作 style 属性。但是,它不会删除样式表或< style> 元素中已应用CSS规则的样式。


< blockquote>

If I set a CSS value on a specific element using:

$('#element').css('background-color', '#ccc');

I want to be able to unset that element-specific value and use the cascaded value, along the lines of:

$('#element').css('background-color', null);

But that syntax doesn't seem to work -- is this possible using another syntax?

Thanks in advance!

Edit: The value isn't inherited from the parent element -- the original values comes from an element-level selector. Sorry for any confusion!

解决方案

From the jQuery docs:

Setting the value of a style property to an empty string — e.g. $('#mydiv').css('color', '') — removes that property from an element if it has already been directly applied, whether in the HTML style attribute, through jQuery's .css() method, or through direct DOM manipulation of the style property. It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or <style> element.

这篇关于如何使用jQuery取消设置元素的CSS属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆