jQuery callbacks.has()方法
jQuery中的callbacks.has()方法是用来回答列表中是否有任何回调附件。如果一个回调被作为一个参数传递,那么它回答它是否在列表中。
语法:
callbacks.has([callback])
参数:
- callback。该参数定义了要搜索的回调,在列表中。
返回值:该方法要么返回真,要么返回假。
例子1:这个例子返回 “true”,因为’func’在列表中。
<!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.has() method
</p>
<button onclick="Geeks();">
click here
</button>
<p id="GFG"></p>
<script>
var el_down = document.getElementById("GFG");
var res = "";
var callbacks = jQuery.Callbacks();
function Geeks() {
var func = function (val) {
res = res + "value passed is - " + val;
};
callbacks.add(func); // function added to list
callbacks.fire("gfg_1");
el_down.innerHTML = callbacks.has();
}
</script>
</body>
</html>
输出:
例子2:这个例子返回 “false”,因为’func2’不在列表中。
<!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.has() 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() {
var func1 = function (val) {
res = res + "value passed is - " + val;
};
var func2 = function (val) {
res = res + "value passed is - " + val;
};
// Function added to list
callbacks.add(func1);
callbacks.fire("gfg_1");
el_down.innerHTML = callbacks.has(func2);
}
</script>
</body>
</html>
输出: