Home > Web Front-end > Front-end Q&A > How to modify the CSS style of an element using jQuery

How to modify the CSS style of an element using jQuery

PHPz
Release: 2023-04-24 10:31:32
Original
1192 people have browsed it

In front-end development, the importance of style is self-evident. How to dynamically modify the style of elements is also one of the skills that front-end developers must master. jQuery is one of the most widely used JavaScript libraries in front-end development. It provides a series of APIs for manipulating DOM elements, including modifying the style of elements. This article will introduce how to use jQuery to modify the CSS style of elements.

1. Basic knowledge

Before we start learning how to use jQuery to modify the CSS style of elements, we need to understand some basic knowledge first.

  1. CSS Selector

The CSS selector is used to select elements in an HTML document. It defines a pattern for matching HTML elements. In jQuery, we can use CSS selectors to select elements that need to be modified.

For example, if you want to select all p elements, you can use the following code:

$('p')
Copy after login

If you want to select the element with the id "example", you can use the following code:

$('#example')
Copy after login

If you want to select an element with class "example", you can use the following code:

$('.example')
Copy after login
  1. CSS style

CSS style defines the appearance and layout of the element. We can set styles for HTML elements in CSS files, or modify the styles of elements through JavaScript.

For example, if you want to set the background color of an element, you can use the following CSS style:

background-color: red;
Copy after login

If you want to set the font size of an element, you can use the following CSS style:

font-size: 14px;
Copy after login

2. Modify CSS style

There are two methods to use jQuery to modify element CSS style:

  1. .css() method

.css The () method is used to get or set the CSS properties of the element. We can use this method to modify the CSS style of the element.

For example, if you want to modify the background color of the p element, you can use the following code:

$('p').css('background-color', 'red');
Copy after login

If you want to modify multiple CSS styles at the same time, you can pass an object containing multiple properties and values ​​as parameter.

For example, if you want to modify the background color and font size of the p element, you can use the following code:

$('p').css({
  'background-color': 'red',
  'font-size': '14px'
});
Copy after login
  1. .addClass() and .removeClass() methods
  2. ## The
#.addClass() method is used to add one or more classes to an element, where class refers to a collection of CSS styles. We can use this method to add styles to elements.

For example, if you want to add a class named "red" to the p element, you can use the following code:

$('p').addClass('red');
Copy after login
If you want to add multiple classes to the p element at the same time, you can pass a containing Strings of multiple class names are used as parameters, and the class names are separated by spaces.

For example, if you want to add classes named "red" and "bold" to the p element, you can use the following code:

$('p').addClass('red bold');
Copy after login
.removeClass() method is used to remove a class from the element or multiple classes.

For example, if you want to delete the class named "red" from the p element, you can use the following code:

$('p').removeClass('red');
Copy after login
If you want to delete multiple classes from the p element at the same time, you can pass a A string containing multiple class names is used as a parameter, and the class names are separated by spaces.

For example, if you want to delete the classes named "red" and "bold" from the p element, you can use the following code:

$('p').removeClass('red bold');
Copy after login
3. Example

We can An example is used to demonstrate how to use jQuery to modify the CSS style of elements. Let's say we have a button, when the button is clicked, it changes the background color of the p element to red and adds a class called "bold". When the button is clicked again, it removes the class named "red" of the p element.

HTML part:

<button id="btn">Click me</button>
<p id="para">Hello, jQuery!</p>
Copy after login
JavaScript part:

$(document).ready(function() {
  $('#btn').click(function() {
    $('#para').css('background-color', 'red').addClass('bold');
    if ($('#para').hasClass('red')) {
      $('#para').removeClass('red');
    } else {
      $('#para').addClass('red');
    }
  });
});
Copy after login
In this example, we use the .click() method to add a click event handler to the button. When the button is clicked, it will use the .css() method to change the background color of the p element to red, and use the .addClass() method to add a class named "bold" to the p element. Then, it checks whether the p element already has a class named "red", and if it already does, it removes it using the .removeClass() method; otherwise, it uses the .addClass() method to add a class named "red" The class is added to the p element. This way, every time the button is clicked, the style of the p element will change.

Summary

This article introduces how to use jQuery to modify the CSS style of elements. We can use the .css() method to set the CSS property of an element to a specified value, and we can also use the .addClass() and .removeClass() methods to add or remove one or more classes from an element. In actual development, we can choose different methods to modify the style of elements according to specific needs.

The above is the detailed content of How to modify the CSS style of an element using jQuery. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template