prevAll([selector]) trong jQuery



Miêu tả

Phương thức prevAll([selector]) trong jQuery tìm tất cả phần tử anh em ở trước phần tử hiện tại.

Cú pháp

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

selector.prevAll( [selector] )

Tham số

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

  • selector − Selector tùy ý này để lọc các phần tử ở trước.

Quảng cáo

Ví dụ

Ví dụ đơn giản sau minh họa cách sử dụng của prevAll([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(){
            $("div:last").prevAll().addClass("hilight");
         });
      </script>
		
      <style>
         .hilight { background:yellow; }
      </style>

   </head>
	
   <body>
      <div>first</div>
      <div>sibling<div>child</div></div>
      <div>sibling</div>
      <div>sibling</div>
   </body>
	
</html>
Quảng cáo

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(){
            $("div:last").prevAll().addClass("hilight");
         });
      </script>
		
      <style>
         .hilight { background:yellow; }
      </style>

   </head>
	
   <body>
      <div class="hilight">first</div>
      <div class="hilight">sibling<div>child</div></div>
      <div class="hilight">sibling</div>
      <div>sibling</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