is(selector) trong jQuery



Miêu tả

Phương thức is(selector) trong jQuery kiểm tra sự chọn lọc hiện tại với một Expression và trả về true, nếu ít nhất một phần tử của sự chọn lọc đó phù hợp với selector đã cho.

Nếu không có phần tử nào phù hợp, hoặc bộ chọn là không hợp lệ, thì phản hồi sẽ là 'false'.

Cú pháp

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

element.is( selector )

Tham số

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

  • selector − Expression để lọc.

Quảng cáo

Ví dụ

Ví dụ đơn giản sau minh họa cách sử dụng của is(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() {
            $("li").click(function () {
				
               if ($(this).is(":first-child")) {
                  $("p").text("This is list item 1");
               } else if ($(this).is(".middle0,.middle1")) {
                  $("p").text("This is middle class list");
               } else if ($(this).is(":contains('item 5')")) {
                  $("p").text("It's 5th list");
               } 
           });
         });
      </script>
		
      <style>
         .selected { color:red; }
      </style>
		
   </head>
	
   <body>
      <div>
         <span>Click any list item below:</span>
			
         <ul>
            <li class="top0">list item 1</li>
            <li class="top1">list item 2</li>
            <li class="middle0">list item 3</li>
            <li class="middle1">list item 4</li>
            <li class="bottom0">list item 5</li>
            <li class="bottom1">list item 6</li>
         </ul>
			
         <p>FILLER</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