仅从内容 HTML 中获取数字

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

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

 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()
        
     }

enter image description here

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

javascript arrays numbers parseint getelementsbyclassname
1个回答
0
投票

试试这个

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.