children([selector]) trong jQuery



Miêu tả

Phương thức children([selector]) trong jQuery nhận một tập hợp các phần tử chứa tất cả các phần tử con trực tiếp duy nhất của mỗi một trong tập hợp các phần tử được so khớp.

Cú pháp

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

selector.children( [selector] )

Tham số

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

  • selector − Đây là tham số tùy ý để lọc tất cả các phần tử con. Nếu không được cung cấp, thì tất cả phần tử con sẽ được chọn.

Quảng cáo

Ví dụ

Ví dụ đơn giản sau minh họa cách sử dụng của children([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>
         $(document).ready(function(){
            $("div").children(".selected").addClass("blue");
         });
      </script>
		
      <style>
         .blue { color:blue; }
      </style>
		
   </head>
	
   <body>
	
      <div>
         <span>Hello</span>
         <p class="selected">Hello Again</p>
         <div class="selected">And Again</div>
         <p class="biggest">And One Last Time</p>
      </div>
		
   </body>
	
</html>

Nó sẽ áp dụng màu blue cho tất cả phần tử con với một class là "selected" của mỗi div, như dưới đây:

<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>
         $(document).ready(function(){
            $("div").children(".selected").addClass("blue");
         });
      </script>
		
      <style>
         .blue { color:blue; }
      </style>
		
   </head>
	
   <body>
	
      <div>
         <span>Hello</span>
         <p class="blue">Hello Again</p>
         <div class="blue">And Again</div>
         <p class="biggest">And One Last Time</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