jQuery callbacks.add()方法

jQuery callbacks.add()方法

jQuery callbacks.add() 方法是用来添加一个回调或回调集合到回调列表中。这个方法返回它所连接的回调对象(this)。
语法:

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); //Adding the func1
            callbacks.fire("GFG_1"); // Calling the fun1
            el_down.innerHTML = res;
        }
    </script>
</body>
 
</html>

输出:

jQuery callbacks.add()方法

例子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

输出:

jQuery callbacks.add()方法

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程