在对象创建时添加事件处理程序不起作用

Adding event handlers on object creation doesn't work?

本文关键字:事件处理 程序 不起作用 添加 对象 创建      更新时间:2023-09-26

>我有以下内容:

MycheckModal.__construct = function(element){
this.blackout = $("<div class='modal-backdrop'></div>")
    .attr("style", 'left:0px;top:0px;position:absolute;background:black')
    .css("opacity", "0.5")
    .css("height", $(document).height() + 'px')
    .css("width", $(document).width() + 'px')
    .css("z-index", "5000");
this.blackout.live("click", function(){
    MycheckModal.__destruct();
});
}
MycheckModal.__destruct = function(){
    this.element = null;
    this.url = null;
    this.blackout.fadeOut(150, function(){ 
        MycheckModal.blackout.remove();
        MycheckModal.blackout = null;
        } );
    this.modal.fadeOut(150, function(){ 
        MycheckModal.modal.remove();
        MycheckModal.modal = null;
        } );    
}

这是一个大一点的代码,但你得到了jist。无论如何 - 事件处理程序没有注册,但是 - 当我显式注册它时 - 在构造函数之外 - 它工作正常。

有什么想法我需要做什么吗?

我不知道为什么,但请尝试使用"点击"而不是"实时"。

this.blackout.click(function() {
    MycheckModal.__destruct();
});
this.blackout.on("click", function(){
    MycheckModal.__destruct();
});