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


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


jQuery中的callbacks.empty()方法用于从列表中删除所有回调。它返回其附加到的Callbacks对象。

用法:

callbacks.empty()

参数:它不接受任何参数。

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

以下示例说明了jQuery中的callbacks.empty()方法:



范例1:在此示例中,首先将fun1函数添加到回调列表,然后使用给定参数在列表中执行所有存在的回调。执行callbacks.empty()方法以清空列表。添加第二个函数fun2并再次执行回调列表。这说明添加第一个函数后清空列表。

html

<!DOCTYPE HTML> 
<html> 
  
<head> 
    <title> 
        JQuery callbacks.empty() method 
    </title> 
  
    <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.empty() method 
    </p> 
  
    <button onclick="Geeks();"> 
        click here 
    </button> 
  
    <p id="output"></p> 
  
    <script> 
        var output = document.getElementById("output"); 
        var res = ""; 
  
        // Initialize a callback list 
        var callbacks = jQuery.Callbacks(); 
  
        function Geeks() { 
              
            // First function to be added to the list 
            var fun1 = function (val) { 
                res = res + "This is function 1 and" + 
                    " value passed is " + val + "<br>"; 
            }; 
  
            // Second function to be added to the list 
            var fun2 = function (val) { 
                res = res + "This is function 2 and" + 
                    " value passed is " + val + "<br>"; 
            }; 
  
            // Adding the first function 
            callbacks.add(fun1); 
  
            // Calling the first function 
            callbacks.fire("GFG_1"); 
  
            // Clearing the callback list 
            callbacks.empty(); 
  
            // Adding the second function 
            callbacks.add(fun2); 
  
            // Calling the first function 
            callbacks.fire("GFG_2"); 
  
            output.innerHTML = res; 
        }  
    </script> 
</body> 
  
</html>

输出:

范例2:本示例提供了一个按钮,用于清空回调列表,然后添加给定函数以查看清空回调列表的结果。

html

<!DOCTYPE HTML> 
<html> 
  
<head> 
    <title> 
        JQuery | callbacks.empty() method 
    </title> 
  
    <script src= 
"https://code.jquery.com/jquery-3.5.1.min.js"> 
    </script> 
</head> 
  
<body style="text-align:center;"> 
  
    <h1 style="color:green;"> 
        GeeksForGeeks 
    </h1> 
  
    <p> 
        JQuery | callbacks.empty() method 
    </p> 
  
    <button onclick="Geeks();"> 
        click here 
    </button> 
  
    <button onclick="empty();"> 
        empty 
    </button> 
      
    <p id="output"></p> 
  
    <script> 
        var output = document.getElementById("output"); 
        var res = ""; 
  
        // Initialize a callback list 
        var callbacks = jQuery.Callbacks(); 
  
        function empty() { 
  
            // Clear the callback list 
            callbacks.empty(); 
        } 
  
        // Function to add and fire callbacks  
        function Geeks() { 
  
            // Function to be added to the list 
            var fun1 = function (val) { 
                res = res + "This is function 1 and" + 
                    " value passed is " + val + "<br>"; 
            }; 
  
            // Adding the given function 
            callbacks.add(fun1); 
  
            // Calling the function with value 
            callbacks.fire("GFG_1"); 
  
            output.innerHTML = res; 
        }  
    </script> 
</body> 
  
</html>

输出:




相关用法


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