[英]Toggle button back to default html
我正在嘗試將按鈕的innerHTML更改回默認值。 它可以與第一次更改innerHTML一起使用,但是后來我無法將其恢復為默認設置。
我嘗試使用布爾值來回切換,但是我的代碼只是不會執行該函數的第二部分。
function money() { var money = document.getElementById('money'); var text = "normal"; if (text == "normal") { money.innerHTML = "<h1>Let me ask you</h1><p>Does this work<p>"; text = "changed"; } else { money.innerHTML = "<h1>Money Laundering</h1><p>Click For More Info</p>"; text = "normal"; } }
<div id="practiceContainer"> <h1 id="practiceHeader">Practice Areas</h1> <div class="lawgrid"> <button class="practicesBox" id="money" onclick="money()"> <h1>Money Laundering</h1> <p>Click For More Info</p> </button> </div> </div>
我希望我的代碼可以更改回默認的html,但是不會發生。
您可以在函數開始時將text
變量設置為"normal"
,因此,當代碼到達if
邏輯時, text
將始終為"normal"
。
考慮將text
變量簡單地移到函數外 ,以免每次單擊時都將其重置。
var text = "normal"; var btnMoney = document.getElementById('money'); function money() { if (text == "normal") { btnMoney.innerHTML = "<h1>Let me ask you</h1><p>Does this work<p>"; text = "changed"; } else { btnMoney.innerHTML = "<h1>Money Laundering</h1><p>Click For More Info</p>"; text = "normal"; } }
<div id="practiceContainer"> <h1 id="practiceHeader">Practice Areas</h1> <div class="lawgrid"> <button class="practicesBox" id="money" onclick="money()"> <h1>Money Laundering</h1> <p>Click For More Info</p> </button> </div> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.