当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JQuery unbind()用法及代码示例


unbind()方法是jQuery中的内置方法,用于删除任何选定的事件处理程序。此方法可用于删除特定的事件处理程序,或停止特定的函数。它适用于使用事件对象的任何事件处理程序。

注意:如果未提供任何参数,则该方法适用于指定元素的所有事件处理程序。
用法:

$(selector).unbind(event, function, eventObj)

参数:此方法接受上述和以下所述的三个参数:


  • event:它是一个可选参数,用于指定事件(一个或多个)以将其从元素中删除。
  • function:它是一个可选参数,用于指定要与元素的指定事件解除绑定的函数的名称。
  • eventObj:它是一个可选参数,用于指定要从事件绑定函数中删除的事件对象。

示例1:本示例介绍了unbind()方法,用于从选定元素中删除事件处理程序。

<!DOCTYPE html>   
<html>   
  
<head>  
    <title>  
        jQuery unbind() Method 
    </title>  
      
    <script src= 
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
    </script> 
 </head>  
         
 <body style = "text-align:center;">   
     
    <h1 style = "color:green;" >   
        GeeksForGeeks   
    </h1>   
             
    <button>  
        Click Here  
    </button>  
          
    <!-- Script to illustrates unbind() method -->
    <script> 
        $(document).ready(function() { 
            $("h1").click(function() { 
                $(this).slideToggle(); 
            }); 
              
            $("button").click(function() { 
                $("h1").unbind(); 
            }); 
        }); 
    </script> 
</body>   
  
</html> 

输出:

  • 单击任何位置之前:
  • 单击元素h1后:
  • 单击按钮后,事件将不起作用:

示例2:本示例介绍了unbind()方法,用于从选定元素中删除事件处理程序。

<!DOCTYPE html>   
<html>   
  
<head>  
    <title>  
        jQuery unbind() Method 
    </title>  
      
    <script src= 
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
    </script> 
      
    <style> 
        h1 { 
            border: 1px solid black; 
            height: 100px; 
            padding-top: 35px; 
            background: green; 
            color: white; 
        } 
    </style> 
</head>  
         
<body style = "text-align:center;">   
     
    <h1>GeeksForGeeks</h1>   
      
    <button>  
        Remove event handler from geeks for geeks 
    </button>  
      
    <!-- Script to illustrates unbind() method -->   
    <script> 
        $(document).ready(function() { 
            $("h1").click(function() { 
                $(this).slideToggle(); 
            }); 
              
            $("button").click(function() { 
                $("h1").unbind(); 
            }); 
        }); 
    </script> 
</body>   
  
</html> 

输出:

  • 单击任何位置之前:
  • 单击元素h1后:
  • 单击按钮后,事件将不起作用:


相关用法


注:本文由纯净天空筛选整理自ashishsaini3大神的英文原创作品 jQuery | unbind() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。