简体   繁体   English

jQuery,将css属性设置为空字符串

[英]jquery, setting css property to empty string

I'm currently studying the jquery css() method. 我目前正在研究jquery css()方法。

In the .css() documentation, it states .css()文档中,它指出

在此处输入图片说明

However, I'm not exactly sure what they meant by used to cancel any style modification you have previously performed. 但是,我不确定它们用来取消您之前执行的任何样式修改的含义 It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or element. 但是,它不会删除样式表或元素中已应用CSS规则的样式。 .

So I looked up MDN page on HTMLElement.style , which states 所以我在HTMLElement.style上查找了MDN页面

在此处输入图片说明

I also experiment by setting one of the property of HTMLElement.style to "" (ie an empty string) 我还通过将HTMLElement.style的属性之一设置为"" (即空字符串)进行实验

在此处输入图片说明

So it seems that by setting color property of d.style to "" , I have removed the value of color property, and the color property was not removed from d.style . 因此,似乎通过将d.style color属性设置为"" ,我删除了color属性的值,并且未从d.style删除color属性。

But what does the documentation mean by It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or element. 但是,文档的含义是什么,但是,它并未删除样式表或元素中已应用CSS规则的样式。

The .css() method can remove inline styles (denoted by style="" ): .css()方法可以删除内联样式(由style="" ):

 var target = document.getElementById('target'); $('#target').css('color', ''); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="target" style="color: red">Target</div> 

But not styling rules that have been defined in a CSS stylesheet: 不是 CSS样式表中已定义的样式规则:

 var target = document.getElementById('target'); $('#target').css('color', ''); 
 #target { color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="target">Target</div> 

Nor will it work for styling inside <style> tags written in the same file: 在同一文件中编写的<style>标签内部进行样式化也不起作用:

 var target = document.getElementById('target'); $('#target').css('color', ''); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="target">Target</div> <style> #target { color: red; } </style> 

This is because the CSSStyleDeclaration object is a read-only interface to Window.GetComputedStyle() , which is used to retrieve the stylesheets. 这是因为CSSStyleDeclaration对象Window.GetComputedStyle()只读接口,该接口用于检索样式表。

The Window.getComputedStyle() method returns an object that reports the values of all CSS properties of an element after applying active stylesheets and resolving any basic computation those values may contain. 应用活动样式表并解析这些值可能包含的所有基本计算之后Window.getComputedStyle()方法将返回一个对象,该对象报告元素的所有CSS属性的值。

Element.style returns a CSSStyleDeclaration object, and is also read-only: Element.style返回CSSStyleDeclaration对象,并且也是只读的:

Styles should not be set by assigning a string directly to the style property (as in elt.style = "color: blue;" ), since it is considered read-only, as the style attribute returns a CSSStyleDeclaration object which is also read-only. 不应通过直接将字符串分配给style属性来设置样式(如elt.style = "color: blue;" ),因为它被认为是只读的,因为style属性返回一个CSSStyleDeclaration对象,该对象也可以读为-只要。

jQuery's css() method itself attempts to modify the style property, and thus it can only update inline styles: jQuery的css()方法本身试图修改style属性,因此它只能更新内联样式:

When using .css() as a setter, jQuery modifies the element's style property. 当使用.css()作为设置器时,jQuery会修改元素的style属性。 [ ... ] Setting the value of a style property to an empty string — eg $( "#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. [...]将样式属性的值设置为空字符串(例如$( "#mydiv" ).css( "color", "" ) ,如果该属性已经被直接应用 ,则会从该元素中删除该属性,无论是在HTML样式属性中,通过jQuery的.css()方法还是通过对样式属性进行直接DOM操作。 [ ... ] It does not, however, remove a style that has been applied with a CSS rule in a stylesheet or element. [...]但是,它不会删除样式表或元素中已应用CSS规则的样式。

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

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