3 回答

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊
您的條件不正確,您需要使用 '==' 之類的
if (document.getElementById("toggle").style.transform == "rotate(0deg)")
正確的功能是這樣的——
function togglepage() {
if (document.getElementById("toggle").style.transform == "rotate(0deg)" || document.getElementById("toggle").style.transform == "") {
document.getElementById("toggle").style.transform = "rotate(90deg)";
}
else if (document.getElementById("toggle").style.transform == "rotate(90deg)") {
document.getElementById("toggle").style.transform = "";
}
}

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊
請(qǐng)嘗試此代碼
https://codepen.io/dkreativetech/pen/yLeEKoY
代碼
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<div class="dropdown">
<button class="dropdown-toggle" type="button">
DROPDOWN
<div class="caret">?</div>
</button>
</div>
jQuery代碼
$(".dropdown-toggle").on("click", function () {
$(".caret").toggleClass("active");
})
CSS 代碼
.dropdown .dropdown-toggle {
color: #fff;
background-color: #337ab7;
border-color: #2e6da4;
border: 1px solid transparent;
padding: 6px 20px 6px 12px;
font-size: 14px;
display: flex;
align-items: center;
justify-content: center;
}
.dropdown-toggle .caret {
margin-left: 10px
}
.dropdown-toggle .caret.active {
transform: rotate(90deg);
}

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊
我希望這有幫助
let toggle = document.getElementById("toggle");
function togglepage(toggle) {
console.log(toggle);
switch(toggle.style.transform){
case "":
case "rotate(180deg)":
case "rotate(0deg)":
toggle.style.transform = "rotate(90deg)";
break;
case "rotate(90deg)":
toggle.style.transform = "rotate(180deg)";
break;
}
}
添加回答
舉報(bào)