从列表中仅获取数字内容 HTML [重复]

问题描述 投票:0回答:1

目前我正在用 Javascript 启动一个项目,它是一个游戏卡,我只想从列表中获取数字 没有字符串,只有整数 text

enter image description here

我只想从 HTMl 列表中获取数字,而不是字符串。我尝试转换为 parseInt 但我知道什么时候将元素作为数组返回,只是类的数量我没有内容

power = document.getElementsByClassName("power").innerText;
let opNum0 = parseInt(document.getElementsByClassName("power")[0].innerText);
let myNum3 = parseInt(document.getElementsByClassName("power")[3].innerText);
console.log(myNum3);

if (myNum3 > opNum0) {
  alert("you win");

} else {

  alert(myNum3);
  scoreCountYou()

}

javascript arrays numbers parseint getelementsbyclassname
1个回答
-2
投票

试试这个

let opNum0 = parseInt(document.getElementsByClassName("power")[0].innerText.replace(/\D/g,''));
let myNum3 = parseInt(document.getElementsByClassName("power")[3].innerText.replace(/\D/g,''));
console.log(myNum3);

if(myNum3 > opNum0){
    alert("you win");
}else{
    alert(myNum3);
    scoreCountYou()
}
© www.soinside.com 2019 - 2024. All rights reserved.