2016-03-19 50 views
0

我試圖隱藏div .first隨時.second是可見的。現在我正在使用z-index來隱藏它們,但一直不成功。如何隨時隱藏.first。第二次出現?如何隱藏另一個可見的div?

Website Reference

CSS

.highlight { 
opacity: 100; 
z-index: -1; 
} 

.second { 
z-index: 1;  
} 

HTML

<div class="toggleElements"> <!---AdServer---> 
<div class="first" id="adserver_contain"> 
<div id="ad_server"><img class="highlight" src="Images/Adserver_roll.png"></div> 
</div> 


<div class="second" id="ad_serverb"> 
<img class="img-responsive" src="Images/Adserver.png"> 
<div id="ad_serverb_text"><h1>Ad Server</h1><p> 
Ad servers aggregate data and provide a centralized reporting interface. This aggregate determines what publishers sites get what inventory. AdsNative is the only truly independent ad server.</p></div> 
</div> 


</div> <!---AdServer End---> 

當前的Jquery

$(document).ready(function() { 
    $(".toggleElements").each(function() { 
    var parent = $(this); 
    $(this).find(".first").click(function() { 
     $(this).fadeToggle(); 
     $(parent).find(".second").fadeToggle(); 
    }); 
    $(this).find(".second").click(function() { 
     $(this).fadeToggle(); 
     $(parent).find(".first").fadeToggle(); 
    }); 
    }); 
}); 
+0

*「我正在使用z-index隱藏」* - 爲什麼你使用z-index這個? – nnnnnn

+0

我以爲我可以簡單地將圖像類轉換回來,所以如果不使用javascrpt –

回答

0

這是你需要什麼?在單擊的項目上顯示圖像並將其隱藏在所有其他項目中?

$(document).ready(function() { 
    var toggleElements = $(".toggleElements"), 
     clickAbleDivs = toggleElements.find(' > div'), 
     currDiv, otherDivs, img; 

    $(clickAbleDivs).each(function(){ 
     $(this).on('click', function(){ 
     currDiv = $(this),  
     img = $(currDiv).find('img').show(); 

     otherDivs = $(clickAbleDivs).not(currDiv); 
     otherDivs.each(function(){ 
      $(this).find('img').hide(); 
     }); 
     })  
    }); 
}); 
相關問題