jQuery callbacks.fireWith()方法

jQuery callbacks.fireWith()方法

jQuery中的callbacks.fireWith()方法是用来调用所有当前在列表中给定上下文和参数的回调。

语法:

callbacks.fireWith([context][, params])

参数:

  • context。这个参数定义了列表中的回调应被触发的上下文的引用。
  • params。这个参数是一个数组或类似数组的对象,用来传递给列表中的回调的参数。如果不使用或未定义,将不传递参数。

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

实例1:本实例使用上下文 “window”,并将参数传递给函数。

<!DOCTYPE HTML>
<html>
  
<head>
    <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.fireWith() method
    </p>
      
    <button onclick="Geeks();">
        click here
    </button>
      
    <p id="GFG_DOWN"></p>
      
    <script>
        var el_down = document.getElementById("GFG_DOWN");
  
        var res = "";
        var callbacks = jQuery.Callbacks();
  
        function Geeks() {
  
            // First function to be added to the list
            var func = function (val) {
                res = res + "value passed is - " + val;
            };
  
            // Add the function func to
            // the callbacks list
            callbacks.add(func);
  
            // Fire the callbacks on the list
            // using the context "window"
            // and an parameters array
            callbacks.fireWith(window, ["gfg_1"]);
            el_down.innerHTML = res;
        } 
    </script>
</body>
  
</html> 

输出:
jQuery callbacks.fireWith()方法

实例2:本例使用上下文 “window”,并将2个参数传递给函数。

<!DOCTYPE HTML>
<html>
  
<head>
    <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.fireWith() method
    </p>
      
    <button onclick="Geeks();">
        click here
    </button>
      
    <p id="GFG_DOWN"></p>
  
    <script>
        var el_down = document.getElementById("GFG_DOWN");
        var res = "";
        var callbacks = jQuery.Callbacks();
  
        function Geeks() {
  
            // first function to be added to the list
            var func = function (val_1, val_2) {
                res = res + "values passed are - "
                        + val_1 + ", " + val_2;
            };
  
            // Add the function func to
            // the callbacks list
            callbacks.add(func);
  
            // Fire the callbacks on the
            // list using the context "window"
            // and an parameters array
            callbacks.fireWith(window, ["gfg_1", "gfg_2"]);
            el_down.innerHTML = res;
        } 
    </script>
</body>
  
</html>

输出:
jQuery callbacks.fireWith()方法

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程