jQuery click事件触发两次

时间:2012-07-09 23:26:03

标签: javascript jquery

我正在开发的内容叠加脚本存在问题。 看来我的结束事件会触发两次,但第一次(或第二次,根据您点击的开启链接)返回'undefined'。

您可以在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);

3 个答案:

答案 0 :(得分:3)

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

您将两个对象close()方法绑定到close处理程序。基本上,当您单击关闭按钮时,它会运行两个函数,每个函数对应一个覆盖对象。由于一个叠加层对象不存在,它将返回undefined

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

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

DEMO: http://jsfiddle.net/UhSLy/9/

答案 1 :(得分:0)

如果我建议请查看:http://jsfiddle.net/PgbfN/25/

我认为这个if条件是检查插件是否已初始化undefined因此会运行两次。

要解决,我添加了isApplied标志,一旦应用,请将标记设置为true。 Rest希望演示将有助于:)

希望有所帮助

$.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);

答案 2 :(得分:0)

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

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

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

我认为代码解释了一切。如果实例不存在,close事件将永远不会触发。

DEMO.