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


jQuery callbacks.remove()用法及代码示例


jQuery callbacks.remove()方法用于从回调列表中删除单个回调或一组回调。

句法:

callbacks.remove( callbacks )

参数:

  • callbacks:此参数指定要从回调列表中删除的一个函数或一组函数。

返回值:此方法返回其附加到的Callbacks对象。

范例1:在此示例中,存在一种删除方法,用于从列表中删除函数‘func’。



html

<!DOCTYPE HTML> 
<html> 
  
<head> 
    <script src= 
"https://code.jquery.com/jquery-3.5.0.js"> 
    </script> 
</head> 
  
<body style="text-align:center;"> 
    <h1 style="color:green;"> 
        GeeksForGeeks 
    </h1> 
  
    <p> 
        jQuery | callbacks.remove() method 
    </p> 
  
    <button onclick="Geeks();"> 
        click here 
    </button> 
  
    <p id="GFG"></p> 
  
    <script> 
        var el_down = document.getElementById("GFG"); 
        var res = ""; 
        var callbacks = jQuery.Callbacks(); 
  
        function Geeks() { 
            var func = function (val) { 
                res = res + "value passed is - " + val; 
            }; 
  
            // Function added to list 
            callbacks.add(func); 
            callbacks.fire("gfg_1"); 
  
            // Removing the func from list 
            callbacks.remove(func); 
              
            // Now This will not work 
            callbacks.fire("gfg_2"); 
            el_down.innerHTML = res; 
        }  
    </script> 
</body> 
  
</html>

输出:

范例2:本示例提供了一个按钮,用于从“回调”列表中删除函数‘fun’。

html

<!DOCTYPE HTML> 
<html> 
  
<head> 
    <script src= 
"https://code.jquery.com/jquery-3.5.0.js"> 
    </script> 
</head> 
  
<body style="text-align:center;"> 
  
    <h1 style="color:green;"> 
        GeeksForGeeks 
    </h1> 
  
    <p> 
        JQuery | callbacks.remove() method 
    </p> 
  
    <button onclick="Geeks();"> 
        click here 
    </button> 
      
    <button onclick="remove();"> 
        remove 
    </button> 
      
    <p id="GFG"></p> 
  
    <script> 
        var el_down = document.getElementById("GFG"); 
        var res = ""; 
        var callbacks = jQuery.Callbacks(); 
  
        var fun = function (val) { 
            res = res + "This is function and " 
                + "value passed is " + val + "<br>"; 
        }; 
  
        // Adding function to Callback list 
        callbacks.add(fun); 
          
        // Defining function to remove 
        function remove() { 
            callbacks.remove(fun); 
        } 
          
        function Geeks() { 
            callbacks.fire("GFG_1"); 
            el_down.innerHTML = res; 
        }  
    </script> 
</body> 
  
</html>

输出:该函数已从回调列表中删除。




相关用法


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