off(events,[selector],[handler(eventObject)]) trong jQuery



Miêu tả

Phương thức off(events,[selector],[handler(eventObject)]) trong jQuery thực hiện ngược lại với on(), nó gỡ bỏ một sự kiện sống được bind.

Cú pháp

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

selector.on( event, selector, handler ) 

Tham số

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

  • events − Kiểu sự kiện phân biệt nhau bởi khoảng trống.

  • selector − Một Selector String

  • handler − Một hàm để bind tới sự kiện trên mỗi phần tử trong tập hợp các phần tử đã so khớp.

Quảng cáo

Ví dụ

Ví dụ đơn giản sau minh họa cách sử dụng của off(events,[selector],[handler(eventObject)]) 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() {

            function aClick() {
               $("div").show().fadeOut("slow");
            }
				
            $("#bind").click(function () {
               $("#theone").on("click", aClick).text("Can Click!");
            });
				
            $("#unbind").click(function () {
               $("#theone").off("click", aClick).text("Does nothing...");
            });
         });
      </script>
		
      <style>
         button { margin:5px; }
         button#theone { color:red; background:yellow; }
      </style>
		
   </head>
	
   <body>
	
      <button id="theone">Does nothing...</button>
      <button id="bind">Bind Click</button>
      <button id="unbind">Unbind Click</button>
		
      <div style="display:none;">Click!</div>
		
   </body>
	
</html>
Quảng cáo

Nó sẽ cho kết quả:

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


xu_ly_su_kien_trong_jquery.jsp