jquery clicks on child element to add style

WBOY
Release: 2023-05-28 16:00:38
Original
738 people have browsed it

How to use jQuery to click on child elements and add styles

jQuery is a popular JavaScript library that allows JavaScript code to be written in a way that improves code simplicity and readability. jQuery has a series of convenient DOM manipulation functions that make it easier to manipulate elements on the page. In this article, I will show you how to use jQuery to click on child elements and add styles.

Step 1: Introduce jQuery

Add the jQuery library to your HTML file. You can download it from the official website or connect directly to jQuery’s CDN in your HTML file. Here we take CDN as an example:

Copy after login

Step 2: Write HTML and CSS code

Prepare an HTML page with multiple sub-elements that will be clicked. At the same time, set the style in CSS to distinguish the style changes after clicking. For example:

子元素1
子元素2
子元素3
Copy after login
.child {
  padding: 10px;
  border: 1px solid black;
  cursor: pointer;
}

.clicked {
  background-color: yellow;
}
Copy after login

Here, we set three child elements that will change their background color when they are clicked.

Step 3: Write jQuery code

We need to write some code to handle what happens when the child element is clicked. First, we need to select all child elements. Using jQuery's child element selector, we can easily select all child elements under the parent element:

var children = $('#parent > .child');
Copy after login

Next, we need to add click event listeners for each child element. We can do this using jQuery's each() function. This function accepts a callback function that will be applied to each element. In this callback function, we will add a click event listener to increase the style of the child element:

children.each(function() {
  $(this).on('click', function() {
    $(this).addClass('clicked');
  });
});
Copy after login

Here, we use jQuery's on() function to add a click event listener. This function accepts two parameters. The first one is the event type, we used "click". The second parameter is the callback function, which is executed when the click responds. In this callback function, we call the addClass() function on the child element to add the "clicked" class to the child element. We define a "clicked" class that will change the background color of child elements.

The complete jQuery code is as follows:

$(document).ready(function() {

  var children = $('#parent > .child');

  children.each(function() {
    $(this).on('click', function() {
      $(this).addClass('clicked');
    });
  });

});
Copy after login

Finally, we put all the code in the $(document).ready() function and execute them after the DOM is fully loaded.

Conclusion

In this article, we have learned how to use jQuery to click on child elements and add styles. We need to select all the child elements and then add a click event listener for each child element. In the callback function, we can use the addClass() function to add a class to the child element, so that we can add various styles to our child elements. If you want to add other attributes of child elements, you can also learn and practice in click event listeners.

The above is the detailed content of jquery clicks on child element to add style. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!