2013-12-17 26 views
0

我有一些獨特的ID和相同類的div mbox。這些div擁有一些信息。在每個div的底部,我有一個與所有div相同的類的div removemeFadeout每個div分開

當我點擊removeme類的div以淡化類mbox的div時,該怎麼辦?但是,只有mbox,而不是其他旁邊

我的HTML:

<style> 

.mbox{ 
width:300px; 
height:280px; 
float:left; 
margin-left:5px; 
margin-right:10px; 
margin-bottom:10px; 
background-color: #E0E0E0; 
border-radius: 5px; 
-moz-border-radius: 5px; 
-webkit-border-radius: 5px; 
border:1px solid #CACACA; 
} 

.removeme{ 
width:200px; height:45px; float:left; background-color: #F00;  
} 

.title{ 
width:290px; 
float:left; 
margin-left:5px; 
margin-top:5px; 
text-align:center; 
color:#333; 
font-family:Verdana, Geneva, sans-serif; 
font-size:24px; 
font-weight:bold; 
} 

.photoholder{ 
width:200px; 
height:150px; 
float:left; 
margin-left:50px; 
margin-top:8px; 
background-color:#FFF;  
} 

.imgclass{ 
float:left; 
margin-left:10px; 
margin-top:5px;  
} 
</style> 


<div class="mbox" id="1"> 
<div class="title">Hello Box</div> 
<div class="photoholder"> 
<img class="imgclass" src="http://whomurderedrobertwone.com/wp-content/uploads/2010/06/BigStarlitSky-300x250.jpg" width="180" height="140"> 
</div> 
<div style="width:200px; height:45px; float:left; margin-top:12px; margin-left:50px; cursor:pointer;"> 
<div class="removeme"></div> 
</div>  
</div> 

<div class="mbox" id="2"> 
<div class="title">Hello Box</div> 
<div class="photoholder"> 
<img class="imgclass" src="http://whomurderedrobertwone.com/wp-content/uploads/2010/06/BigStarlitSky-300x250.jpg" width="180" height="140"> 
</div> 
<div style="width:200px; height:45px; float:left; margin-top:12px; margin-left:50px; cursor:pointer;"> 
<div class="removeme"></div> 
</div>  
</div> 

<script type="text/javascript"> 
$('.removeme').click(function() { 
$(this).fadeOut("fast"); 
}); 
</script> 

檢查我的演示:http://jsfiddle.net/fWtm6/9/

+0

你應該提供相關的代碼中的問題,以及提供爵士 –

+0

代碼! –

+2

只需使用'$(this).closest('。mbox')。fadeOut(「fast」);'。 – candeias

回答

2

您可以使用.parent()獲取父元素(在這種情況下,你需要2〜獲取父.mbox

$('.removeme').click(function() { 
    $(this).parent().parent().fadeOut(); 
}); 

http://jsfiddle.net/kkd3r/

編輯:第二看jQuery的API .parents()後會是一個更好的主意,因爲它遍歷所有的父元素,所以你可以明確地通過class和id篩選掉元素不管是多少了樹

$('.removeme').click(function() { 
    $(this).parents('.mbox').fadeOut(); 
}); 

http://jsfiddle.net/kkd3r/1/

0

試試這個,希望它會回答你的問題..

$('.removeme').click(function() { 
    var str=$(this).parent().closest(".mbox"); 
$(str).fadeOut("fast"); 
}); 
0

替換該行

$(this).fadeOut("fast"); 

與此

$(this).closest('.mbox').fadeOut("fast");