尽管使用myViewBack.BackgroundColor = UIColor.FromName("MyColor1");
或NaN
,我仍然获得了parseInt
,但我不明白为什么会这样。我也试过更改代码,但后来我得到了.value
。
作为一个初学者,尽管它如此简单,但我并没有得到我做错的事。有什么提示吗?
undefined

const btn = document.querySelector('.btn');
btn.addEventListener('click',calcWinner);
function calcWinner(e){
const powerG = document.querySelector('#power-g');
const techG = document.querySelector('#tech-g');
const chanceG = document.querySelector('#chance-g');
const powerF = document.querySelector('#power-f');
const techF = document.querySelector('#tech-f');
const chanceF = document.querySelector('#chance-f');
let gokuR = parseInt(powerG + techG + chanceG)
let friezeR = parseInt(powerF.value) + parseInt(techF.value) + parseInt(chanceF.value);
console.log(parseInt(gokuR.value));
}

答案 0 :(得分:1)
首先,抓取值而不是元素,例如像这样:
const powerG = document.querySelector('#power-g').value;
然后使用 parseInt
, Number
或 < em> +
将String
号码转换为实际号码,您就可以计算出结果。
Stack snippet
const btn = document.querySelector('.btn');
btn.addEventListener('click',calcWinner);
function calcWinner(e){
const powerG = document.querySelector('#power-g').value;
const techG = document.querySelector('#tech-g').value;
const chanceG = document.querySelector('#chance-g').value;
const powerF = document.querySelector('#power-f').value;
const techF = document.querySelector('#tech-f').value;
const chanceF = document.querySelector('#chance-f').value;
let gokuR = Number(powerG) + Number(techG) + Number(chanceG);
let friezeR = parseInt(powerF) + parseInt(techF) + parseInt(chanceF);
console.log(gokuR);
console.log(friezeR);
}
<div class="container">
<div class="goku">
<h2>Battle chance</h2>
<form action="">
<input type="number" name="" id="power-g" >
<label for="power-g">Power</label>
<input type="number" name="" id="tech-g" >
<label for="tech-g">Technique</label>
<input type="number" name="" id="chance-g" >
<label for="chance-g">Chance</label>
</form>
</div>
<div class="frieza">
<h2>Battle chance</h2>
<form action="">
<input type="number" name="" id="power-f" >
<label for="power-f">Power</label>
<input type="number" name="" id="tech-f" >
<label for="tech-f">Technique</label>
<input type="number" name="" id="chance-f" >
<label for="chance-f">Chance</label>
</form>
</div>
</div>
<h1 >Winner : <span class="winner"></span></h1>
<a href="#" class="btn">CalcWinner</a>