2011-07-19 28 views
0

如果一個jQuery插件執行該代碼的JavaScript控制檯錯誤:解除綁定click事件我想要做什麼,但火災

dropdown = { 
    doc: $(document), 
    element: $('#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); 

我如何可以禁用/刪除/解除綁定單擊處理我自己的自定義(另一個文件)jquery插件?

我用這個代碼:

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

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

我得到了我想要的東西,但JavaScript控制檯顯示了這個錯誤:

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

請讓我知道是否有避免這種錯誤的方式。

在此先感謝。

回答

0

好吧,我明白了:

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); 
1

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

從你的例子,它看起來像你錯過了在調用unbind屬性名。你不是這個意思嗎?

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

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

通知書dropdown.element.unbind()。變量dropdown不是您示例中的jQuery對象,但是dropwdown.element是。

+0

謝謝扎克,我試了一下,但它不起作用。但它的作用就像是我幾分鐘前發佈的答案。 –

+0

你的意思是它沒有做你想做的事情,或者它仍然會產生一個錯誤? –

+0

現在就做我想要的,沒有錯誤。謝謝Zack。 –