2015-05-05 20 views

回答

2

使用$(obj).is(':visible')來確定對象obj是否已被切換爲「可見」。

使用例如在連接後:

$('.orange').hide(); 
 

 
$('.gray, .orange').on('click', function() { 
 
    $('.gray, .orange').toggle(); 
 

 
    if($('.gray').is(':visible')) { 
 
    $('#output').html('gray'); 
 
    } 
 
    else { 
 
    $('#output').html('orange'); 
 
    }  
 
});
.blue{ 
 
    height:100px; 
 
    width:250px; 
 
    background:#1ecae3; 
 
    float:left; 
 
} 
 
.gray{ 
 
    height:100px; 
 
    width:100px; 
 
    background:#eee; 
 
    float:left;  
 
} 
 

 
.orange{ 
 
    height:100px; 
 
    width:150px; 
 
    background:#fdcb05; 
 
    float:left;  
 
} 
 

 
#output { 
 
    clear: both; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="blue"></div> 
 
<div class="gray"> 
 
    <p> Show --> </p> 
 
</div> 
 
<div class="orange" > 
 
    <p> -- Hide </p> 
 
</div> 
 

 
<div id="output"></div>

+0

偉大的作品!謝謝! –