javascript: document.dispatchEvent(CustomEvent) 什么都不做

javascript: document.dispatchEvent(CustomEvent) does nothing

我有以下代码块

$(function(){

    var TaskEvent = new CustomEvent('TaskAdded',{
        detail: {
            message:'A task fue ponida, champion'
        },
        bubbles:true,
        cancelable:true
    });

    var btn = $("#boton");

    function HandleSugar(e){
        console.log(e.detail.message);
    }

    btn.click(function(e){

        document.dispatchEvent(TaskEvent);
    });

    $(document).on('TaskEvent',HandleSugar);

})

单击按钮 (#boton) 时没有任何反应。有什么想法吗?

一些说明:

您正在监听 TaskEvent 但事件实际上是 TaskAdded

$(document).on('TaskEvent',HandleSugar);

应该是

$(document).on('TaskAdded', HandleSugar);