开发者

Unbind click event does what I want but fires an javascript console error

If one Jquery plugin this code is executed:

dropdown = {
    doc: $(document),
    eleme开发者_开发技巧nt: $('#user_info'),
    open: function() {
        if ( ! dropdown.element.hasClass('active') ) {
            dropdown.element.addClass('active');
            dropdown.doc.one( 'click', dropdown.close );
            return false;
        }
    },
    close: function() {
        dropdown.element.removeClass('active');
    }
};

dropdown.element.click( dropdown.open );

How can I disable/remove/unbind the click handler in my own custom (another file) Jquery plugin?

I was using this code:

dropdown = {
    doc: jQuery(document),
    element: jQuery('#user_info')
};

dropdown.element.click(function(e) {
    dropdown.element.unbind('click', dropdown.open);
});

I get what I want, but javascript console shows this error:

TypeError: Object #<Object> has no method 'unbind'...

Please let me know if there is a way to avoid this error.

Thanks in advance.


Please let me know if there is a way to avoid this error.

From you example it looks like you're missing a property name in your call to unbind. Don't you mean this?

dropdown = {
    doc: jQuery(document),
    element: jQuery('#user_info')
};

dropdown.element.click(function(e) {
    dropdown.element.unbind('click', dropdown.open);
});

Notice dropdown.element.unbind(). The variable dropdown isn't a jQuery object from your example, but dropwdown.element is.


Ok, I get it:

dropdown = {
    doc: jQuery(document),
    element: jQuery('#user_info'),
    open: function() {
        dropdown.element.addClass('active');
        dropdown.doc.one( 'click', dropdown.close );
        return true;
    },
    close: function() {
        dropdown.element.removeClass('active');
    }
};

dropdown.element.click( dropdown.open );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜