var health = document.getElementById("health")
$(document).ready(function(){
$('.heal').click(function(){
var healthval = +document.getElementById('health').getAttribute('value')
console.log(healthval)
if (healthval === 1) {
alert("It's already full!");
} else {
document.getElementById('health').setAttribute('value', healthval + 0.1);
alert("You give your critter some food!");
}
if (healthval > 1) {
healthval = 1;
}
if (healthval === 0) {
alert("Your critter has died! Oh no!");
}
});
});
HTML
<meter low=".5" optimum=".8" high=".7" id="health" value="0.1"></meter>
<button type="button" class="heal">Heal Me</button>
我使用上面的代碼,在 '健康吧'我正在研究一個小項目。 我對此很滿意,因爲它的健康是按照它應有的上/下來的。只是由於某種原因,alert("It's already full!");
和alert("Your critter has died! Oh no!");
部件不應該發生,因爲它們應該是......但每次都會觸發alert("You give your critter some food!");
。
我在做什麼錯?
因爲它總是會在其他部分結束! – 2014-09-19 05:07:35
你有看看你的控制檯嗎? 'healthval'實際上永遠不會變成1或0 ... – webeno 2014-09-19 05:08:57
我一直在[link](http://www.codecademy.com/)上直播這個項目,並且不知道如何查看控制檯。我將如何修改它以使healthval能夠改變? – Asteria 2014-09-19 05:13:43