Jquery method to remove the css attribute of an element: 1. Use the "$(element)" statement to obtain the specified element object; 2. Use the css() method to remove the css attribute of the element. The syntax is "element object .css("property name","");".
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
How to remove the css attribute of an element in jquery
How to remove the css attribute in jquery? If a certain CSS style attribute is added to the dom element of the html, we cannot directly remove these added attributes using jquery, but we can use the style override method to remove the set attributes and restore them to the default style. The following is Let's see how to deal with it.
Since we are using jquery for processing, we will first introduce the jquery plug-in file.
On the html, we create a div with the style my_class and a button to trigger the deletion of the css attribute value.
Coupled with the corresponding style code, here we mainly focus on the set height and color attribute values.
#In the button click event function, we can use jquery’s css method to set the style attributes. For example, if we set the height value here to auto, we can restore the value of the height attribute set in the original style to the default value.
Let’s take a look at the page first. Now the height of the div is 300px. Let’s click the button below.
After clicking, you can see that the height value set by the original style is no longer effective and has been overwritten to the default value of auto, which is equivalent to The original attribute value is deleted.
It should be noted that different style attribute values have different default values, such as color, the default value is inherit,
Refresh the page and click the button again. In addition to the height returning to the default value, the text color also returns to black font, which is equivalent to not setting the color style attribute.
Recommended related video tutorials: jQuery video tutorial
The above is the detailed content of How to remove css attributes of elements in jquery. For more information, please follow other related articles on the PHP Chinese website!