我声明变量:
var Cash_player=0
它在函数中使用:
function send_stats(message) {
document.querySelector("#stats").innerText = message;
}
send_stats(`Stats:
Gold:${Cash_player}
`)
然后在其他功能中增加:
function win() {
Cash_player += 15;
send_stats()
}
但是结果是Undefined
。您能帮我增加价值,然后使用send_stats()
进行显示吗?
答案 0 :(得分:-1)
似乎您想将innerText
更新为Cash_player
,而不必一次又一次地将其传递给function
。您将文本更新为Stats:Gold:${Cash_player}
内的send_stats
let Cash_player = 0;
function send_stats(message) {
document.querySelector("#stats").innerText = `Stats:Gold:${Cash_player}`;
}
function win() {
Cash_player += 15;
send_stats()
}
send_stats()
<div id="stats"></div>
<button onclick="win()">Win</button>