當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。