jQuery事件名称空间如何工作?

 event.namespace属性用于返回自定义命名空间,当事件被触发。

示例

您可以尝试运行以下代码,以了解事件命名空间的工作方式以及如何创建和删除命名空间-

<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.staticfile.org/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>