2017-10-11 96 views
1

如果特定元素具有display:none,我需要更改另一個元素的顯示屬性。如果jQuery中不顯示任何元素,則更改另一個元素的可見性

if ($("#first-layer").css('display') === 'none') { 
 
    $("#second-layer").removeAttr("visibility"); 
 
}
#second-layer { 
 
    color: black; 
 
    background-color: red; 
 
    width: 200px; 
 
    height: 150px; 
 
    background: yellow; 
 
    position: fixed; 
 
    top: 0; 
 
    right: 0; 
 
    bottom: 0; 
 
    left: 0; 
 
    margin: auto; 
 
    visibility: hidden; // here I have to change by jQuery 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<header> 
 
    <div id="first-layer"> 
 
    <div id="header-elements"> 
 
     <div id="img-rain" class="animated fadeIn" class="img"><img src="img/img.png"> </div> 
 
     <div id="typed-strings" class="text"> 
 
     <span class="animated fadeInUp" id="typed"></span> 
 
     <br/> 
 
     <span class="description animated fadeIn">Your weather in one place</span> 
 
     </div> 
 
    </div> 
 
    <div id="typed-strings"> 
 
    </div> 
 
    <div class="start"> 
 
     <button type="button" class="btn btn-primary btn-lg responsive-width button-      bg-clr animated fadeIn">Get Started</button> 
 
    </div> 
 
    </div> 
 
    <div id="second-layer">123</div> 
 

 
</header>

我試圖從互聯網解決方案,但他們沒有工作。

+0

visibility:hidden;是不是一個屬性,所以你不能用removeAttr –

回答

1

你的代碼目前沒有做任何事情,但你可能有

if ($("#first-layer").not(":visible")) { // or .is(":hidden") 
    $("#second-layer").show(); 
} 

$("#second-layer").css({visibility:visible}); 

一個更簡單的時間,因爲CSS聲明是不是屬性

更多檢測這裏 Difference between :hidden and :not(:visible) in jQuery

+0

瞄準它非常感謝你:) 結果: 我在CSS的第二層顯示設置:無和JS我把這個: 如果($(「#第一(「:hidden」)) {(「#second-layer」).css('display','block'); } – Remax

1

使用jQuery(":hidden")

// if selector is none then 
if($('#first-layer').is(":hidden")){ 

    // alter second-layers visibility 
    $('#second-layer').css('visibility', 'visible'); 
} 
相關問題