JS初学者 - 函数和DOM,无法弄清楚我的错误 - 使用DOM作为函数参数



构建一个简单的pokemon类型的弱点计算器-这里是JS+HTML无法理解为什么它不起作用。

我设法让它工作起来,在每个"if/else"语句之后使用innerHTML,但我不明白为什么这不起作用。

let type = document.getElementById("type");
let answer = weakness(type); {
if (type.value === 'fire') {
return ('Rock, ' + 'Water, ' + 'Ground');
} else if (type.value === 'water') {
return ('Grass, ' + 'Electric')
} else if (type.value === 'grass') {
return ('Fire, ' + 'Bug, ' + 'Ice, ' + 'Flying')
}
}
document.getElementById('answer').innerHTML = (answer);
<p>Type:
<select id="type" name="type" placeholder="Select type">
<option value="fire">Fire</option>
<option value="water">Water</option>
<option value="grass">Grass</option>
</select>
<p><input type="button" value="Calulate" onclick="weakness(type)"></p>
<p id="answer"></p>

更正了您的代码错误,请检查。

let type = document.getElementById("type");
const weakness = (type) => {
if (type.value === 'fire') {
return ('Rock, ' + 'Water, ' + 'Ground');
} else if (type.value === 'water') {
return ('Grass, ' + 'Electric')
} else if (type.value === 'grass') {
return ('Fire, ' + 'Bug, ' + 'Ice, ' + 'Flying')
}
}
const calcWeakness = () => {
let answer = weakness(type);
document.getElementById('answer').innerHTML = answer;
}
<p>Type:
<select id="type" name="type" placeholder="Select type">
<option value="fire">Fire</option>
<option value="water">Water</option>
<option value="grass">Grass</option>
</select>
<p><input type="button" value="Calulate" onclick="calcWeakness()"></p>

<p id="answer"></p>

相关内容

  • 没有找到相关文章

最新更新