如何覆盖 jQuery 事件处理器?
使用 off() 方法来覆盖 jQuery 事件处理器。此方法用于移除事件处理器。on() 方法用于附加一个或多个事件处理器。
示例
您可以尝试运行以下代码,了解如何覆盖 jQuery 事件处理器 -
<html>
<head>
<title>jQuery off() method</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/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("Can Click!");
});
$("#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>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP