我想在按鈕上顯示不允許的光標選項。 我想要在javascript中使用CSS來完成此操作,當我的if條件滿足時。如何在JavaScript中使用CSS
我的按鈕代碼是這樣的:
<button type="button" id="myBtn" class="btn btn-success" title=""></button>
這是我的,如果條件:
if (percent <= 99)
{
//using class name:
document.getElementsByClassName("btn").style.cursor = "not-allowed";
}
else
{
//using id:
document.getElementsById("mybtn").style.cursor = "allowed";
}
誰能幫助我,我應該如何使用CSS按鈕在JavaScript中。 – Deb
'getElementsByClassName'返回集合,因此你應該使用'loop'或者如果你的目標是第一個元素,然後'[0]'通過'getElementsByClassName',然後應用樣式..'getElementsById'應該可以正常工作。糾正錯字,它是'getElementById',它是單數的,因爲ID總是唯一的。 – Rayon
僅適用於myBtn: 'myBtn.style.cursor = percent <= 99? 'not-allowed':'default';' –