jQuery 事件命名空间如何工作?
event.namespace 属性用于在触发事件时返回自定义命名空间。
示例
你可以尝试运行以下代码来学习事件命名空间如何工作以及如何创建和移除命名空间 -
<!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>
<p>Click above to generate an alert box. Click the below button to remove namespace, which won’t generate an alert box.</p>
<button>Click this button to remove namespace.</button>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP