亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

基本的 Javascript if 語句與 Jquery 不工作

基本的 Javascript if 語句與 Jquery 不工作

隔江千里 2023-03-18 16:18:07
由于某種原因,這個 if 語句永遠不會到達 else。當我點擊#drawer-drop 時,邊距會發生變化,但我無法將其改回。我沒有看到任何控制臺錯誤。#drawer{margin-top:-600px;}$(document).ready(function(){  $("#drawer-drop").click(function(){    if($("#drawer").css('margin-top','-600px')){       $('#drawer').css('margin-top','0px');    }else{       $('#drawer').css('margin-top','-600px');    }  });});<nav id="drawer">  <h1>This will be a sweet menu.</h1></nav><a id="drawer-drop">MENU</a>
查看完整描述

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>


查看完整回答
反對 回復 2023-03-18
  • 1 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號