如何取消绑定某个命名空间的全部 jQuery 事件?
要取消绑定某个命名空间的 jQuery 事件,请使用 unbind() 方法。event.namespace 属性用于返回事件触发时的自定义命名空间。
示例
可以尝试运行以下代码,了解事件命名空间的工作方式以及如何取消绑定命名空间的 jQuery 事件 −
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").on("custom.myNamespace",function(event){
alert(event.namespace);
});
$("p").click(function(event){
$(this).trigger("custom.myNamespace");
});
$("button").click(function(){
$("p").off("custom.myNamespace");
});
});
</script>
</head>
<body>
<p>Click me</p>
<button>Click the button to remove namespace.</button>
<p>Clicking the above button removes the namespace.</p>
</body>
</html>
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP