Home > Web Front-end > JS Tutorial > How to use find method

How to use find method

不言
Release: 2019-02-12 13:10:26
Original
4464 people have browsed it

find() is a built-in method in jquery that is used to find all descendant elements of the selected element. It will traverse all the way down to the last leaf of the selected element in the DOM tree. Let's take a look at the specific use of find().

How to use find method

The find() method obtains the descendants of each element in the current collection of elements, filtered by a selector, jQuery object, or element.

The basic syntax of find() is as follows

$(selector).find()
Copy after login

selector: It can be written using CSS selector syntax.

Let’s look at a specific example

The code is as follows

<!DOCTYPE html>
<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src="https://code.jquery.com/jquery-3.3.1.min.js">
      </script>
        
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("p").find("span").addClass("selected");
         });
      </script>
        
      <style>
         .selected { color:red; }
      </style>
   </head>
    
   <body>
      <div>
         <p><span>Hello</span>, 你好吗?</p>
         <p>yes, 我<span>很好</span>.</p>
      </div>
   </body>
</html>
Copy after login

The display effect on the browser is as follows: All span elements in the p element are selected and highlighted in red show.

How to use find method

This article ends here. For more related exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to use find method. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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