jQuery callbacks.remove()方法
jQuery callbacks.remove()方法是用来从回调列表中删除一个单一的回调或回调的集合。
语法:
callbacks.remove( callbacks )
参数:
- callbacks。这个参数指定了一个函数,或者一个函数数组,要从回调列表中删除。
返回值:该方法返回它所连接的Callbacks对象。
例子1:在这个例子中,有一个删除方法,用来从列表中删除函数’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.remove() 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;
};
// Function added to list
callbacks.add(func);
callbacks.fire("gfg_1");
// Removing the func from list
callbacks.remove(func);
// Now This will not work
callbacks.fire("gfg_2");
el_down.innerHTML = res;
}
</script>
</body>
</html>
输出:
例子2:这个例子提供了一个按钮,从Callbacks列表中移除函数’fun’。
<!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.remove() method
</p>
<button onclick="Geeks();">
click here
</button>
<button onclick="remove();">
remove
</button>
<p id="GFG"></p>
<script>
var el_down = document.getElementById("GFG");
var res = "";
var callbacks = jQuery.Callbacks();
var fun = function (val) {
res = res + "This is function and "
+ "value passed is " + val + "<br>";
};
// Adding function to Callback list
callbacks.add(fun);
// Defining function to remove
function remove() {
callbacks.remove(fun);
}
function Geeks() {
callbacks.fire("GFG_1");
el_down.innerHTML = res;
}
</script>
</body>
</html>
输出:函数被从回调列表中删除。