2011-02-18 64 views
1

「在此不是這個,而不是一切:」我有一個插件jQuery的選擇

$.fn.dropDown = function(options){ 
    this.each(function(){ 
     var $this= $(this); 
     $(document).click(function(e){ 
      if($(e.target).not($this) && $(e.target).not($this).find('*')){ 
       // do stuff 
      } 
     }); 

    }); 
} 

我要選擇「在此不是這個,而不是一切」,到目前爲止,這是行不通的。

+0

所以澄清:你希望有人從一個元素開始你的插件,然後如果任何其它元素(即未初始化元素或它的後代)是點擊,然後你隱藏你的菜單。是對的嗎? – 2011-02-18 20:33:50

+0

@JacobM我編輯的問題,它更清晰我認爲 – 2011-02-18 20:55:30

回答

4

這應該工作:

var that = this; 
$(document).click(function(e) { 
    if(e.target != that && !$.contains(that, e.target)) { 
     // do stuff 
    } 
});