2012-10-19 29 views
1

我有這個腳本的一個問題: http://jsfiddle.net/bZw83/JQuery的切換滑出選項卡並單擊外面收

如果標籤內發生了點擊它切換或縮小屏幕語言選擇一個選項卡。 現在,我堅持,我試圖寫一個切換的標籤出來的畫面的功能,如果點擊是全光照東西的標籤外像

$('html').click(function() {...}); 

,但我不能從射擊排除選項卡的區域事件。 我試過使用event.stopPropagation(),但我無法實現目標: -/

有人可以幫我弄清楚這一點嗎?

非常感謝您

+0

嘗試使用jquery的選擇器.......試試這個鏈接http://www.w3schools.com/jquery/sel_not.asp –

+0

@raman你能否更新你的評論並選擇'not'作爲源代碼反引號?否則,它*可能被誤解爲「*儘量不要使用jQuery選擇器*」。 :) – insertusernamehere

回答

4

您可以創建文檔的處理程序,然後檢查它是否在#navigation或它的孩子。

SEEE:http://jsfiddle.net/bZw83/1/

$(document).click(function(e) { 
    $target = $(e.target); 
    var is = $target.is('#navigation'); 
    var isChild = $target.parents('#navigation').length; 

    if(!(is) && !(isChild)) { 
     $('a', $(this)).stop().animate({ 
      'marginLeft': '-44px' 
     }, 200); 
     stato = "chiuso"; 
    } 
}); 
+0

你很快。 :) – insertusernamehere

+0

謝謝Marco! 豎起大拇指爲你和很多很多謝謝。 – Emanuele

+1

@insertusername我的生活故事,哈哈! –

0

做這樣的事情

$(document).click(function(e) { 
    if($(e.target).parents("*").andSelf().is('#navigation')==false) 
    { 
     //your code here 
    } 
}); 
0

您可以通過處理文檔的單擊事件做到這一點,如下所示:

$(document).click(function() { 
    $('a',$(this)).stop().animate({'marginLeft':'-44px'},200); 
    stato="chiuso"; 
}); 

見工作demo