jQuery 如何使用jquery仅删除一个样式属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5951365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:05:14  来源:igfitidea点击:

how to remove only one style property with jquery

jquerycss

提问by Kreker

I have a div with this property style="-moz-user-select:none; position:static !important;". I need to remove the -moz-user-selectTried with $(selector).css() but I don't know what value to set because it's "none".

我有一个带有此属性的 div style="-moz-user-select:none; position:static !important;"。我需要删除-moz-user-selectTried with $(selector).css() 但我不知道要设置什么值,因为它是“none”。

回答by Frédéric Hamidi

The documentation for css()says that setting the style property to the empty string will remove that property if it does not reside in a stylesheet:

css()的文档说将 style 属性设置为空字符串将删除该属性,如果它不存在于样式表中:

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.

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

Since your styles are inline, you can write:

由于您的样式是内联的,您可以编写:

$(selector).css("-moz-user-select", "");

回答by dmorlock

You can also replace "-moz-user-select:none" with "-moz-user-select:inherit". This will inherit the style value from any parent style or from the default style if no parent style was defined.

您还可以将“-moz-user-select:none”替换为“-moz-user-select:inherit”。如果没有定义父样式,这将从任何父样式或默认样式继承样式值。