4

我正在开发的内容覆盖脚本有问题。似乎我的关闭事件触发了两次,但第一次返回“未定义”(或第二次,取决于您单击的打开链接)。

您可以在 JSFiddle 上找到一个精简的工作示例:http: //jsfiddle.net/UhSLy/2/

如果您单击1. 单击,然后单击 2. 单击它会先警报undefined,然后再单击Dummy

当我删除一个打开链接时,一切正常。但是我必须有多个链接,因为它们会打开不同的叠加层。

是什么导致了问题,我该如何避免?

编辑:来自 JSFiddle 的代码如下:

;(function ($, window, document, undefined) {

"use strict";

var pluginName = 'contentOverlay',
    defaults = {
        property:   'value'
    };

function Plugin(element, options) {
    this.element = element;
    this.$element = $(element);

    this.options = $.extend({}, defaults, options);

    this.init();
}

Plugin.prototype = {

    /**
     * Init
     */
    init: function () {
        var self = this;

        // Bind opening method
        this.$element.click(function() {
            self.open();
        });

        // Bind closing method
        $('#close').click(function() {
            self.close();
        });
    },

    /**
     * Open
     */
    open: function () {
        this.overlay = 'Dummy';
    },

    /**
     * Close
     */
    close: function () {
        alert(this.overlay); // <==== PROBLEM: fires twice. returns 'undefined' once
    },

};

$.fn[pluginName] = function (options) {
    return this.each(function () {
        if (!$.data(this, 'plugin_' + pluginName)) {
            $.data(this, 'plugin_' + pluginName,
                new Plugin(this, options));
        }
    });
}

$(function () {
    $('.open').contentOverlay();
});

})(jQuery, window, document);

​</p>

4

3 回答 3

3
$('#close').click(function() {
    self.close();
});

您将两个对象close()方法绑定到关闭处理程序。基本上,当您单击关闭按钮时,它会运行两个功能,一个用于每个覆盖对象。因为一个覆盖对象不存在,所以它正在返回undefined.

您可以通过以下方式解决此问题:

close: function () {
    if(this.overlay != undefined){ // Skips over the undefined overlays
        alert(this.overlay);
    }
}

演示:http: //jsfiddle.net/UhSLy/9/

于 2012-07-09T23:33:42.880 回答
0

如果我可以建议在这里查看:http: //jsfiddle.net/PgbfN/25/

我认为您的这种if情况是检查插件是否已初始化,undefined因此会运行两次。

为了解决我添加isApplied的标志,一旦应用将标志设置为true. 休息希望演示会有所帮助:)

希望能帮助到你

$.fn[pluginName] = function(options) {
        return this.each(function() {
            alert('me ==> ' + (!$.data(this, 'plugin_' + pluginName)) + " ==== " + $.data(this, 'plugin_' + pluginName));
            if (!isApplied) {
                $.data(this, 'plugin_' + pluginName, new Plugin(this, options));

            }
        });
    }

完整代码

var isApplied = false;

(function($, window, document, undefined) {

    "use strict";

    var pluginName = 'contentOverlay',
        defaults = {
            property: 'value'
        };

    function Plugin(element, options) {
        this.element = element;
        this.$element = $(element);

        this.options = $.extend({}, defaults, options);
        isApplied = true;
        this.init();
    }

    Plugin.prototype = {

        /**
         * Init
         */
        init: function() {
            var self = this;

            // Bind opening method
            this.$element.click(function() {
                self.open();
            });

            // Bind closing method
            $(document).on('click', '#close', function() {
                alert('Close is clicked');
                //self.close(); //<<== Is called
            });
        },

        /**
         * Open
         */
        open: function() {
            this.overlay = 'Dummy';
        },

        /**
         * Close
         */
        close: function() {
            alert(this.overlay); // <==== PROBLEM: fires twice. returns 'undefined' once
        },

    };

   $.fn[pluginName] = function(options) {
        return this.each(function() {
            alert('me ==> ' + (!$.data(this, 'plugin_' + pluginName)) + " ==== " + $.data(this, 'plugin_' + pluginName));
            if (!isApplied) {
                $.data(this, 'plugin_' + pluginName, new Plugin(this, options));

            }
        });
    }

    $(function() {
        $('.open').contentOverlay();
    });

})(jQuery, window, document);
于 2012-07-09T23:49:13.210 回答
0

我想出了这个(只有这里的变化)

open: function () {
        this.overlay = 'Dummy';
        this.opened=true;
},

$('#close').click(function() {
    if(self.opened) 
    {
        self.opened=false;
        self.close();
    }    
});

I think code explains everything. The close event will never fire if an instance doesn't exist.

DEMO.

于 2012-07-10T00:13:32.307 回答