以下是奖励寿命为1的HTML:
<h3>PLAYER HEALTH<span value="1" id="bonus-life" >1</span></h3>
<progress class="playerHealth health" max="100" value="100">100%</progress>
以下是用于获取跨度值和进度元素的JavaScript:
const playerHealthBar = document.querySelector(".playerHealth");
const bonusLifeEl = document.querySelector("#bonus-life");
console.log("In index.js bonusLifeEl.innerHTML: " + bonusLifeEl.innerHTML);
console.log(
"In index.js - bonusLifeEl value & typeof: " +
bonusLifeEl.value +
" " +
typeof bonusLifeEl.value
);
console.log(
"In index.js - player health value & typeof: " +
playerHealthBar.value +
" " +
typeof playerHealthBar.value
);
我可以获得正确的playerHealthBar值和类型,但当我尝试使用奖励生命时,我得到的都是未定义的,除非我使用innerHTML,否则我会在innerHTML中获得“值”。
控制台日志输出:
In index.js bonusLifeEl.innerHTML: 1
index.js:8 In index.js - bonusLifeEl value & typeof: undefined undefined
index.js:14 In index.js - player health value & typeof: 100 number