1 回答

TA貢獻1829條經驗 獲得超7個贊
if
你的“錯誤”——你在語句中放入了Setter?.css( propertyName, value )
(Set?the value too...),而不是Getter?.css( propertyName )
(Get?the value of...)。
if($("#drawer").css('margin-top')?==?"-5px"){ ??console.log("Do?something"); }
基本片段:
$(document).ready(function(){
? $("#drawer-drop").click(function(){
? ? console.log($("#drawer").css('margin-top'));
? ? if($("#drawer").css('margin-top') == "-5px"){
? ? ? /* Setter */
? ? ? $('#drawer').css('margin-top','0px');
? ? ? $('#drawer').css('background','red');
? ? }else{
? ? ?/* Setter */
? ? ? $('#drawer').css('margin-top','-5px');
? ? ? $('#drawer').css('background','blue');
? ? }
? });
});
#drawer{
? margin-top: 5px;
? background: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<nav id="drawer">
? <h1>This will be a sweet menu.</h1>
</nav>
<a id="drawer-drop" href="#">MENU</a>
添加回答
舉報