【jQuery如何使用callbacks.fire()方法(示例)】jQuery的callbacks.fire()方法用于调用列表中具有给定参数的所有回调。此方法返回其附加到的回调对象(此)。
语法如下:
callbacks.fire( arguments )
参数:
- 参数:此参数定义要传递回回调列表的参数或参数列表。
范例1:此示例添加了fun1()到回调, 然后调用火()方法, 然后再次添加使用不同参数调用回调的相同方法。
<
!DOCTYPE HTML>
<
html >
<
head >
<
title >
jQuery callbacks.fire() method
<
/ title >
<
script src =
"https://code.jquery.com/jquery-3.5.0.js" >
<
/ script >
<
/ head >
<
body style = "text-align:center;
" >
<
h1 style = "color:green;
" >
lsbin
<
/ 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.fire() method";
var result = "";
var callbacks = jQuery.Callbacks();
function Geeks() {// First function to be added to the list
var fun1 = function (val) {
result = result + "This is function 1 "
+ "and value passed is " + val + "<
br >
";
};
// Adding the function 1
callbacks.add(fun1);
// Calling with 'GFG_1'
callbacks.fire("GFG_1");
// Adding the function 1 again
callbacks.add(fun1);
// Calling with argument'GFG_2'
callbacks.fire("GFG_2");
el_down.innerHTML = result;
}
<
/ script >
<
/ body >
<
/ html >
输出如下:
文章图片
范例2:本示例添加了2个不同的函数, 并使用不同的参数调用它们。
<
!DOCTYPE HTML>
<
html >
<
head >
<
title >
JQuery | callbacks.fire() method
<
/ title >
<
script src =
"https://code.jquery.com/jquery-3.5.0.js" >
<
/ script >
<
/ head >
<
body style = "text-align:center;
" >
<
h1 style = "color:green;
" >
lsbin
<
/ 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.fire() method";
var result = "";
var callbacks = jQuery.Callbacks();
function Geeks() {
// function to be added to the list
var fun1 = function (val) {
result = result + "This is function 1 and"
+ " value passed is " + val + "<
br >
";
};
var fun2 = function (val) {
result = result + "This is function 2 and"
+ " value passed is " + val + "<
br >
";
};
callbacks.add(fun1);
// Adding the function 1
callbacks.fire("GFG_1");
// Calling with 'GFG_1'
callbacks.add(fun2);
// Adding the function 2
callbacks.fire("GFG_2");
// Calling with 'GFG_2'
el_down.innerHTML = result;
}
<
/ script >
<
/ body >
<
/ html >
输出如下:
文章图片
推荐阅读
- AngularJS如何使用控制器(代码示例)
- PHP中如何使用多维关联数组(代码实例)
- 亚马逊面试经验分享|S4
- 亚马逊面试经验分享|S5
- 亚马逊面试经验分析和解读|S9
- PHP Ds Sequence filter()函数使用示例
- 算法(如何实现有向无环图(DAG)的拓扑排序())
- 雅虎面试经验分享和解读|S1
- 雅虎面试经验分享和解读|S2