stopImmediatePropagation() trong jQuery



Miêu tả

Phương thức stopImmediatePropagation() trong jQuery dừng phần còn lại của các Handler từ việc được thực thi. Nó cũng dừng bubble bởi gọi event.stopPropagation().

Bạn có thể sử dụng event.isImmediatePropagationStopped() để biết có hay không phương thức này đã được gọi (trên đối tượng Event đó).

Cú pháp

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

event.stopImmediatePropagation() 

Tham số

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

  • NA

Quảng cáo

Ví dụ

Ví dụ đơn giản sau minh họa cách sử dụng của stopImmediatePropagation() 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").click(function(event){
               alert("1 - This is : " + $(this).text());
               // Comment the following to see the effect.
               event.stopImmediatePropagation();
            });
				
            // This won't be executed.
            $("div").click(function(event){
               alert("2 - This is : " + $(this).text());
            });
         });
      </script>
		
      <style>
         div{ margin:10px;padding:12px; border:2px solid #666; width:160px;}
      </style>
		
   </head>
	
   <body>
	
      <p>Click on any box to see the result:</p>
		
      <div id="div1" style="background-color:blue;">
         BOX 1
      </div>
		
      <div id="div2" style="background-color:red;">
         BOX 2
      </div> 
		
   </body>
</html>

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