我正在嘗試更改按鈕功能內部的陣列值。我希望陣列中的第一個數字改變,但似乎無法讓它作業
所以我希望我的 HTML 從此開始:
<button onclick="skillUpgrade([50,5])">Upgrade your skill</button>
對此:
<button onclick="skillUpgrade([55,5])">Upgrade your skill</button>
這是我的 javascript
function skillUpgrade(el) {
roundDown = Math.floor(money / el[0]);
if (memes >= el[0] * checkTimes) {
baseClicks = baseClicks (el[1] * checkTimes);
money = money - (el[0] * checkTimes);
newPrice = el[0] * 1.1;
el[0] = newPrice;
console.log(el[0])
document.getElementById("moneyAmount").innerHTML = money;
}
}
uj5u.com熱心網友回復:
使用行內事件處理程式通常不是一個好主意。這是一個 最小的可重現示例。它使用資料屬性在按鈕中存盤值并使用事件委托來處理按鈕單擊。
document.addEventListener(`click`, handle);
function handle(evt) {
// do something if the click originated from button#upgradeSkill
if (evt.target.id === "upgradeSkill") {
const btn = evt.target;
const currentValue = btn.dataset.values.split(`,`)
.map(Number);
// ^ convert values to Number
currentValue[0] = 5;
// reset the values
// note: the array is automatically converted back to string
btn.dataset.values = currentValue;
}
}
#upgradeSkill:after {
content: ' (current 'attr(data-values)')';
}
<button id="upgradeSkill" data-values="50,5">Upgrade your skills</button>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/405729.html
標籤:
上一篇:由于“未定義”值而無法檢索資料
