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


jQuery callbacks.add()用法及代碼示例

jQuery callbacks.add()方法用於向回調列表添加回調或一組回調。此方法返回其附加到的回調對象(此)。

用法:

callbacks.add(callbacks)

參數:

  • callbacks:此參數包含要添加到回調列表中的一個函數或一組函數。

範例1:此方法將方法fun1()添加到回調並調用它。

<!DOCTYPE HTML> 
<html> 
  
<head> 
    <title> 
        jQuery callbacks.add() 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 id="GFG_UP"></p> 
      
    <button onclick="Geeks();"> 
        click here 
    </button> 
      
    <p id="GFG_DOWN"></p> 
      
    <script> 
        var el_up = document.getElementById("GFG_UP"); 
        var el_down = document.getElementById("GFG_DOWN"); 
        el_up.innerHTML =  
                    "JQuery | callbacks.add() method"; 
        var res = ""; 
          
        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>"; 
            }; 
            var callbacks = jQuery.Callbacks(); 
            callbacks.add(fun1); //Aadding the func1 
            callbacks.fire("GFG_1"); // Calling the fun1 
            el_down.innerHTML = res; 
        }  
    </script> 
</body> 
  
</html>

輸出:

範例2:本示例將方法fun1()和fun2()添加到回調中,然後對其進行調用。請注意,第二次調用fire()方法時,它將使用相同的參數“ GFG_2”來調用這兩個函數。

<!DOCTYPE HTML> 
<html> 
  
<head> 
    <title> 
        jQuery callbacks.add() 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 id="GFG_UP"></p> 
      
    <button onclick="Geeks();"> 
        click here 
    </button> 
      
    <p id="GFG_DOWN"></p> 
      
    <script> 
        var el_up = document.getElementById("GFG_UP"); 
        var el_down = document.getElementById("GFG_DOWN"); 
        el_up.innerHTML =  
                "JQuery | callbacks.add() method"; 
        var res = ""; 
  
        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>"; 
            }; 
  
            var callbacks = jQuery.Callbacks(); 
  
            // Adding the function 1 
            callbacks.add(fun1); 
  
            // Calling the function 1 
            callbacks.fire("GFG_1"); 
  
            // Adding the function 2 
            callbacks.add(fun2);  
  
            // Calling the function 2 
            callbacks.fire("GFG_2");  
  
            // res of the both functions 
            el_down.innerHTML = res;  
        }  
    </script> 
</body> 
  
</html

輸出:




相關用法


注:本文由純淨天空篩選整理自PranchalKatiyar大神的英文原創作品 jQuery callbacks.add() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。