find(selector) trong jQuery



Miêu tả

Phương thức find(selector) trong jQuery tìm kiếm các phần tử con mà so khớp với selector đã cho.

Cú pháp

Sau đây là cú pháp để sử dụng find(selector) trong jQuery:

selector.find( selector )

Tham số

Chi tiết tham số được sử dụng trong phương thức find(selector) trong jQuery:

  • selector − Selector này có thể được viết bởi sử dụng cú pháp CSS 1-3 selector.

Quảng cáo

Ví dụ

Ví dụ đơn giản sau minh họa cách sử dụng của find(selector) trong jQuery.

<html>
   <head>
      <title>The jQuery Example</title>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.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>, how are you?</p>
         <p>Me? I'm <span>good</span>.</p>
      </div>
   </body>
	
</html>

Nó sẽ cho kết quả:

<html>
   <head>
      <title>The jQuery Example</title>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.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 class="selected">Hello</span>, how are you?</p>
         <p>Me? I'm <span class="selected">good</span>.</p>
      </div>
   </body>
	
</html>

Nó sẽ cho kết quả:

Các bài học jQuery khác tại VietJack:


truy_cap_dom_trong_jquery.jsp