使用该off()方法重写jQuery事件处理程序。此方法用于删除事件处理程序。该on()方法用于附加一个或多个事件处理程序。
您可以尝试运行以下代码,以了解如何覆盖jQuery事件处理程序-
<html>
<head>
<title>jQuery off() method</title>
<script src = "https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
function aClick() {
$("div").show().fadeOut("slow");
}
$("#bind").click(function () {
$("#theone").on("click", aClick).text("可以点击!");
});
$("#unbind").click(function () {
$("#theone").off("click", aClick).text("Does nothing...");
});
});
</script>
<style>
button {
margin:5px;
}
button#theone {
color:red;
background:yellow;
}
</style>
</head>
<body>
<button id = "theone">Does nothing...</button>
<button id = "bind">Bind Click</button>
<button id = "unbind">Unbind Click</button>
<div style = "display:none;">Click!</div>
</body>
</html>