Home >Web Front-end >JS Tutorial >Usage example of setting element style using jQuery_jquery
The example in this article describes the use of jQuery to set element styles. Share it with everyone for your reference. The specific analysis is as follows:
CSS is a part of the page that cannot be separated. jQuery also provides some practical methods related to CSS. In the previous article, addClass() was used to add css styles to elements. Here we mainly introduce how jQuery sets the style of the page. Including adding, deleting, dynamic switching, etc.
1. Add and delete css categories.
removeClass corresponds to the addClass method, and the examples will not be repeated here.
2. Dynamically switch between categories.
Many times, depending on the user's operation status, you want the style of some elements to switch between a certain category, sometimes addClass() category, sometimes removeClass() category, Jq provides a direct toggleClass(name) to do this Similar operations.
3. Get and set styles directly.
Exactly similar to the attr() method, jQuery provides the css() method to directly obtain and set the style of an element, such as using css(name) to obtain the style value of a certain style. Set multiple styles at the same time through the css (properties) list, and set a certain style of the element through css (name, value).
For example: modify the color mark by setting the mouseover and mouseout events to trigger css(name,value).
The css method provides the opacity attribute. And compatible with various browsers.
Modify the above example and set the transparency value of the p element through mouse events.
In addition, the hasClass(name) method is provided in css to determine whether an element has a certain css category set. Returns a Boolean value. For example:
Looking at the jQuery source code, the hasClass method is the is() method.
I hope this article will be helpful to everyone’s jQuery programming.