你看,當我第一次點擊按鈕時,它增加了6個而不是5個,之后它每次都只增加1個。我不知道如何解決這個問題,因為我對javascript一無所知。當它達到100美分時,有沒有可能讓它變成1美元,然后如果你再次點擊它,就變成1.01美元,然后是1.02美元,等等。
。var number = 5
function plusOne(value) {
number 。
count.textContent = number.toString()。
}
#coin {
background-image: url("http://pngimg.com/uploads/coin/coin_PNG36868.png"/span>);
border: none;
background-position: center;
background-repeat: no-repeat;
background-size: 200px 200px;
background-color: white;
width: 200px;
height: 200px;
}
#coin:active {
width: 190px;
height: 190px;
background-size: 190px 190px;
}
<!DOCTYPE html>
<html>
<head>
<title>Coin Clicker</title>
</head>/span>
</html>
<html>
<body>
<div>
<span id="count">/span>0< /span><span>¢</span>
</div>/span>
<div>/span>
< button id="coin" onclick="plusOne(value)" alt="coin"> </button>>
</div>
</body>
</html>/span>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
如果想從1開始,將var number改成1
然后把這個添加到函式中:
number = (number*100 1) /100。
增加0.01,而不是number ,因為它將在每次點擊時增加1。
你可能會問為什么不簡單地使用number = number 0.01,原因是當使用小數點時,結果并不準確,你可以通過改變這個運算式看到。所以只是用了一些簡單的數學。
var number = 1
function plusOne(value) {
number = (number*100 1)/100 ;
count.textContent = number.toString()。
}
#coin {
background-image: url("http://pngimg.com/uploads/coin/coin_PNG36868.png"/span>);
border: none;
background-position: center;
background-repeat: no-repeat;
background-size: 200px 200px;
background-color: white;
width: 200px;
height: 200px;
}
#coin:active {
width: 190px;
height: 190px;
background-size: 190px 190px;
}
<!DOCTYPE html>
<html>
<head>
<title>Coin Clicker</title>
</head>/span>
</html>
<html>
<body>
<div>
<span id="count">/span>1< /span><span>¢</span>
</div>/span>
<div>/span>
< button id="coin" onclick="plusOne(value)" alt="coin"> </button>>
</div>
</body>
</html>/span>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
將變數從 "0 "開始,因為當你點擊按鈕時,腳本已經將 5添加到變數中。通過使用number = number 5;
如果你想得到第二個問題的答案,請用它來打開第二個問題,并展示解決第二個問題的嘗試。
。var number = 0;
function plusOne(value) {
number = number 5;
count.textContent = number.toString()。
}
#coin {
background-image: url("http://pngimg.com/uploads/coin/coin_PNG36868.png"/span>);
border: none;
background-position: center;
background-repeat: no-repeat;
background-size: 200px 200px;
background-color: white;
width: 200px;
height: 200px;
}
#coin:active {
width: 190px;
height: 190px;
background-size: 190px 190px;
}
<!DOCTYPE html>
<html>
<head>
<title>Coin Clicker</title>
</head>/span>
</html>
<html>
<body>
<div>
<span id="count">/span>0< /span><span>¢</span>
</div>/span>
<div>/span>
< button id="coin" onclick="plusOne(value)" alt="coin"> </button>>
</div>
</body>
</html>/span>
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/321020.html
標籤:
