我想使圖像出現在div,當它完成if
條件但它不起作用。JavaScript,圖像不會出現
那麼錯誤在哪裏呢?或者JS代碼如何工作?
的jsfiddle這裏:https://jsfiddle.net/wu6emhh7/29/
有代碼
<div class="row">
<div class="col-md-6">
<h2>BMI Rechner</h2>
<p>Ermitteln Sie Ihren BMI</p>
<form id="bmiForm" name="bmiForm">
<div>
<label for="height"><strong>Grösse</strong> [cm]
<input id="height" onkeyup="calculateBMI();" name="height" size="6" type="text" value="170" /> </label>
<br />
<label for="weight"><strong>Gewicht</strong> [kg]
<input id="weight" onkeyup="calculateBMI();" name="weight" size="6" type="text" value="71" /> </label>
<br />
<input onclick="calculateBmi()" type="button" value="BMI berechnen" />
<br /> <strong>Ihr BMI</strong>
<input name="bmi" size="10" type="text" />
<br /> <strong>Einteilung:</strong>
<input name="meaning" size="20" type="text" />
<br />
<input type="reset" value="Zurücksetzen" /> </div>
</form>
</div>
<div class="contener">
<div id="Empty1"></div>
<div id="Empty2"></div>
<div id="Empty3"></div>
<div id="Empty4"></div>
</div>
<div class="contener2">
<div class="subcontener1"> 18.5 </div>
<div class="subcontener2"> 25 </div>
<div class="subcontener3"> 30 </div>
</div>
<div class="contener">
<div class="untergewicht"> Untergewicht </div>
<div class="normalgewicht"> Normalgewicht </div>
<div class="uebergewicht"> Übergewicht </div>
<div class="adipositas"> Adipositas </div>
</div>
function calculateBmi() {
var weight = document.bmiForm.weight.value
var height = document.bmiForm.height.value
if (weight > 0 && height > 0) {
var finalBmi = Math.round((weight/(height/100 * height/100)) * 10)/10;
document.bmiForm.bmi.value = finalBmi;
if (finalBmi < 18.4) {
document.bmiForm.meaning.value = "Untergewicht";
onload = function f() {
document.getElementById("Empty1").innerHTML = '<img src="http://www.iconsdb.com/icons/preview/black/arrow-204-xxl.png"/>';
}
}
if (finalBmi > 18.5 && finalBmi < 24.9) {
document.bmiForm.meaning.value = "Normalgewicht, weiter so!"
onload = function f() {
document.getElementById("Empty2").innerHTML = '<img src="http://www.iconsdb.com/icons/preview/black/arrow-204-xxl.png"/>';
};
}
if (finalBmi > 25 && finalBmi < 29.9) {
document.bmiForm.meaning.value = "Übergewicht"
onload = function f() {
document.getElementById("Empty3").innerHTML = '<img src="http://www.iconsdb.com/icons/preview/black/arrow-204-xxl.png"/>';
};
}
if (finalBmi > 30) {
document.bmiForm.meaning.value = "Adipositas, lassen Sie sich professional beraten"
onload = function f() {
document.getElementById("Empty4").innerHTML = '<img src="http://www.iconsdb.com/icons/preview/black/arrow-204-xxl.png"/>';
};
}
} else {
alert("Bitte alles ausfüllen");
}
}
'onload'應該做什麼?顯示的代碼從來沒有調用它,所以當然它沒有做任何事情。既然你從來沒有宣佈它,我猜你正試圖操縱'window.onload'?這樣做什麼都不做,負載'事件'已經在頁面加載時運行,然後你可以在表單中輸入任何內容。當您重新加載頁面時,'load'事件將被清除並且不會運行。如果你只是刪除函數並操作'if'主體中的元素,它將會執行你似乎正在嘗試做的事情。 –