2010-08-19 39 views
1

我用這個快速片段與圖像關閉show/hideDIV關閉只有目標對象

$('a.close').click(function() { 
    $('#timeline-2010-1').hide(); 
    $('#timeline-2010-2').hide(); 

    return false; 
    }); 

的問題是,當我關閉一個箱子都在附近的箱子...

有沒有辦法對此進行修改,以便當您單擊該特定的圖像「x」時,只有該圖像關閉,並且不會關閉所有圖像?

回答

0

嘗試是沿着這樣:

$('a.close').click(function() { 
    $(this).hide(); 
    return false; 
    }); 

這裏的事情是指特定對象的this

1

像這樣:

$('a.close').click(function() { 
    $(this).closest('.Timeline').hide(); 

    return false; 
}); 

$(this).closest('.Timeline')會發現,包含點擊的元素的.Timeline元素。您應該根據需要更換.Timeline選擇器。

1

此代碼將關閉父圖像的div,你點擊:

$("div img").click(function(){ 
    $(this).closest("div").hide(); 
}); 

與您DIVID S,這將是:

$("div[id^='timeline-2010-'] img").click(function(){ 
    $(this).closest("div").hide(); 
});