2017-01-23 190 views
1

完全困惑,因爲這應該是如此簡單。我有一個jQuery的選項卡結構。在第一個標籤中,我插入了一個顯示爲黃色框的div容器。在這個黃色的盒子裏面,我試圖插入一個div。顯示爲紅色框的容器。如何將div容器放在另一個div容器內,在標籤內

但我不能讓紅框出現在黃框內。我已經嘗試了通常的定位和z-index等,但奇怪的是沒有任何工作。我想我已經對這個明顯的問題失明瞭。

小提琴:https://jsfiddle.net/k1kphcm8/

$('.tabs-nav a').on('click', function(event) { 
 
    event.preventDefault(); 
 

 
    $('.tab-active').removeClass('tab-active'); 
 
    $(this).parent().addClass('tab-active'); 
 
    $('.TabContainerClass div').hide(); 
 
    $($(this).attr('href')).fadeIn(300) 
 
}); 
 

 
$('.tabs-nav a:first').trigger('click'); // Default
.tabs-nav { 
 
    list-style: none; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
.tabs-nav .tab-active a { 
 
    background: white; 
 
    font-size: 13px; 
 
    border-width: 1px; 
 
    border-bottom-color: white; 
 
    border-top-color: darkorange; 
 
    border-left-color: darkorange; 
 
    border-right-color: darkorange; 
 
} 
 
.tabs-nav a { 
 
    border-width: 0px 1px 1px 0px; 
 
    border-style: solid; 
 
    border-bottom-color: darkorange; 
 
    border-right-color: #C9C9C9; 
 
    background: #E6E6E6; 
 
    color: #7A7A7A; 
 
    display: block; 
 
    font-size: 12px; 
 
    height: 32px; 
 
    line-height: 32px; 
 
    text-align: center; 
 
    width: 122px; 
 
} 
 
.tabs-nav li { 
 
    float: left; 
 
} 
 
.TabContainerClass { 
 
    width: 491px; 
 
    height: 250px; 
 
    border: 1px solid darkorange; 
 
    border-top: 0; 
 
    clear: both; 
 
    position: relative; 
 
    background: white; 
 
} 
 
.YellowDivClass { 
 
    position: absolute; 
 
    background-color: yellow; 
 
    width: 350px; 
 
    height: 200px; 
 
    margin: 30px 0px 0px 20px; 
 
    z-index: 1; 
 
} 
 
.RedDivClass { 
 
    position: absolute; 
 
    background-color: red; 
 
    z-index: 10; 
 
    top: 0px; 
 
    left: 0px; 
 
    width: 50px; 
 
    height: 50px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 

 
<ul class="tabs-nav"> 
 
    <li class="tab-active"><a href="#YellowDiv" rel="nofollow">Countries</a> 
 
    </li> 
 

 
    <li class=""><a href="#tab-2" rel="nofollow">Year</a> 
 
    </li> 
 
    <li class=""><a href="#tab-3" rel="nofollow">Materials</a> 
 
    </li> 
 
    <li class=""><a href="#tab-4" rel="nofollow">Products</a> 
 
    </li> 
 
</ul> 
 

 

 
<div class="TabContainerClass"> 
 

 
    <div id="YellowDiv" class="YellowDivClass"> 
 
    <div id="RedDiv" class="RedDivClass"></div> 
 
    </div> 
 

 

 
    <div id="tab-2" style="display: none;"> 
 
    <p>This is TAB 2</p> 
 
    </div> 
 

 
    <div id="tab-3" style="display: none;"> 
 
    <p>This is TAB 3.</p> 
 
    </div> 
 

 
    <div id="tab-4" style="display: none;"> 
 
    <p>This is TAB 4.</p> 
 
    </div> 
 

 
</div>

回答

2

要隱藏你正在使用的所有非活動標籤

$('.TabContainerClass div').hide(); 

其中隱藏內部TabContainerClass WHI 所有的div ch沒有打算。而是用它來只隱藏直接孩子TabContainerClass:您選擇的

$('.TabContainerClass > div').hide(); 

UPDATED FIDDLE

$('.tabs-nav a').on('click', function(event) { 
 
    event.preventDefault(); 
 

 
    $('.tab-active').removeClass('tab-active'); 
 
    $(this).parent().addClass('tab-active'); 
 
    $('.TabContainerClass > div').hide(); 
 
    $($(this).attr('href')).fadeIn(300) 
 
}); 
 

 
$('.tabs-nav a:first').trigger('click'); // Default
.tabs-nav { 
 
    list-style: none; 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
.tabs-nav .tab-active a { 
 
    background: white; 
 
    font-size: 13px; 
 
    border-width: 1px; 
 
    border-bottom-color: white; 
 
    border-top-color: darkorange; 
 
    border-left-color: darkorange; 
 
    border-right-color: darkorange; 
 
} 
 
.tabs-nav a { 
 
    border-width: 0px 1px 1px 0px; 
 
    border-style: solid; 
 
    border-bottom-color: darkorange; 
 
    border-right-color: #C9C9C9; 
 
    background: #E6E6E6; 
 
    color: #7A7A7A; 
 
    display: block; 
 
    font-size: 12px; 
 
    height: 32px; 
 
    line-height: 32px; 
 
    text-align: center; 
 
    width: 122px; 
 
} 
 
.tabs-nav li { 
 
    float: left; 
 
} 
 
.TabContainerClass { 
 
    width: 491px; 
 
    height: 250px; 
 
    border: 1px solid darkorange; 
 
    border-top: 0; 
 
    clear: both; 
 
    position: relative; 
 
    background: white; 
 
} 
 
.YellowDivClass { 
 
    position: absolute; 
 
    background-color: yellow; 
 
    width: 350px; 
 
    height: 200px; 
 
    margin: 30px 0px 0px 20px; 
 
    z-index: 1; 
 
} 
 
.RedDivClass { 
 
    position: absolute; 
 
    background-color: red; 
 
    z-index: 10; 
 
    top: 0px; 
 
    left: 0px; 
 
    width: 50px; 
 
    height: 50px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 

 
<ul class="tabs-nav"> 
 
    <li class="tab-active"><a href="#YellowDiv" rel="nofollow">Countries</a> 
 
    </li> 
 

 
    <li class=""><a href="#tab-2" rel="nofollow">Year</a> 
 
    </li> 
 
    <li class=""><a href="#tab-3" rel="nofollow">Materials</a> 
 
    </li> 
 
    <li class=""><a href="#tab-4" rel="nofollow">Products</a> 
 
    </li> 
 
</ul> 
 

 

 
<div class="TabContainerClass"> 
 

 
    <div id="YellowDiv" class="YellowDivClass"> 
 
    <div id="RedDiv" class="RedDivClass"></div> 
 
    </div> 
 

 

 
    <div id="tab-2" style="display: none;"> 
 
    <p>This is TAB 2</p> 
 
    </div> 
 

 
    <div id="tab-3" style="display: none;"> 
 
    <p>This is TAB 3.</p> 
 
    </div> 
 

 
    <div id="tab-4" style="display: none;"> 
 
    <p>This is TAB 4.</p> 
 
    </div> 
 

 
</div>

+1

很好斑點。非常感謝你的幫助。 – Silverburch

+0

歡迎您:) – kukkuz

0

的問題之一。您隱藏每個div元素而不是隱藏直接div元素。所以而不是$('.TabContainerClass div').hide();。你應該有$('.TabContainerClass > div').hide();

相關問題