jquery - 如何使用 jQuery 取消设置元素的 CSS 属性?

标签 jquery css

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

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

我希望能够取消设置特定于元素的值并使用级联值,按照以下行:

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

但该语法似乎不起作用——是否可以使用其他语法?

编辑:该值不是从父元素继承的——原始值来自元素级选择器。抱歉造成任何混淆!

最佳答案

来自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 - 如何使用 jQuery 取消设置元素的 CSS 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/490910/

相关文章:

javascript - 从超链接单击添加文本框和标签

javascript - 使用理想的图像 slider ,但仅将图像保留在屏幕上

javascript - 有没有一种方法可以删除 JavaScript/JQuery 所做的所有 CSS 更改

CSS 选择器匹配样式属性中的特定值

jquery - html css拖放文件上传样式

php - 如何阻止 Firefox 关闭我的流式 jQuery ajax 响应?

javascript - 从表中的 JSON 数组获取数据

html - 如何合并 HTML 输入框和按钮? (附上样本图片)

css - 在悬停时更改图像移动图像

php - 如何在 php echo 输出中使用 css 样式