jQuery callbacks.empty()方法

jQuery callbacks.empty()方法

jQuery中的callbacks.empty()方法是用来删除一个列表中的所有回调。它返回它所连接的Callbacks对象。

语法:

callbacks.empty()

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

返回值:该方法返回它所连接的Callbacks对象。

下面的例子说明了jQuery中的callbacks.empty()方法。

例子1:在这个例子中,fun1函数首先被添加到回调列表中,所有存在的回调都在列表中用一个给定的参数执行。执行callbacks.empty()方法来清空该列表。第二个函数fun2被添加,回调列表再次被执行。这展示了在添加第一个函数后清空列表的情况。

<!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>

输出:

jQuery callbacks.empty()方法

例子2:这个例子提供了一个按钮来清空回调列表,然后添加给定的函数来查看清空回调列表的结果。

<!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>

输出:

jQuery callbacks.empty()方法

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程