简体   繁体   English

Javascript BMI计算器在IE中不起作用

[英]Javascript BMI calculator doesn't work in IE

My BMI calculator works in Chrome, Firefox and Opera but not in IE. 我的BMI计算器可以在Chrome,Firefox和Opera中使用,但不能在IE中使用。 When I look at the IE Debugger, there are no errors. 当我查看IE调试器时,没有错误。 The problem is that the calculator doesn't show the result. 问题是计算器不显示结果。 I'm aware that my question is similar to this link Inline event handler not working in JSFiddle however, I can't understand the solution in relation to my problem. 我知道我的问题类似于此链接Inline事件处理程序无法在JSFiddle运行,但是,我无法理解与我的问题有关的解决方案。

Here is the javascript: 这是JavaScript:

const form = document.querySelector('form[name=bmi]');  
const onSubmit = event => {

event.preventDefault();
let healthMessage;

const result = form.querySelector('.result');
const health = form.querySelector('.health');

const weight = parseInt(form.querySelector('input[name=weight]').value, 10);
const height = parseInt(form.querySelector('input[name=height]').value, 10);    
const bmi = (weight / (height /100 * height / 100)).toFixed(1);

if (bmi < 18.5) {
  healthMessage = 'undervægtig';
} else if (bmi > 18.5 && bmi < 25) {
  healthMessage = 'normal vægtig';
} else if (bmi > 25) {
  healthMessage = 'overvægtig';
}
result.innerHTML = bmi;
health.innerHTML = healthMessage;
};  
form.addEventListener('submit', onSubmit, false); 

Here is the HTML: 这是HTML:

<form name="bmi">
<h1>Mål dit BMI:</h1>
<label>
<input type="text" name="weight" id="weight" placeholder="Vægt (kg)">
<input type="text" name="height" id="height" placeholder="Højde (cm)">
<input type="submit" name="submit" id="submit" value="Udregn BMI">
</label>
<div class="calculation">
<div>
  Dit BMI er: <span class="result"></span>
</div>
<div>
  Dette betyder at du er: <span class="health"></span>
</div>
</div>
</form>

And here is a fiddle: https://jsfiddle.net/1nd7oot5/ 这是一个小提琴: https : //jsfiddle.net/1nd7oot5/

IE is too old to handle arrow functions => , from JavaScript's ES2015 edition. IE太老了,无法处理JavaScript ES2015版本中的箭头函数=> You can see this from the IE 'F12 Developer Tools'. 您可以从IE“ F12开发人员工具”中看到这一点。

Consider whether you need to support IE at all. 考虑是否需要支持IE。 The code works in Microsoft's current browser, Edge. 该代码可在Microsoft当前的浏览器Edge中运行。 If you really want to support IE, switch the arrow function out for function(){} 如果您确实要支持IE,请将箭头功能切换为function(){}

PS. PS。 You can also swap: 您还可以交换:

parseInt(someString, 10)

with

Number(someString)

To make the code easier to read. 使代码更易于阅读。 You might also wan to add constants for the 18.5 number and the 25 number to indicate what they mean. 您可能还想为18.5和25的数字添加常量,以表明它们的含义。

Here's a working jsfiddle . 这是一个工作的jsfiddle However unless you have the extra time or money to support out of date browsers (which, as a learner, you probably don't) I would avoid supporting them and keep your current code. 但是,除非您有额外的时间或金钱来支持过时的浏览器(作为学习者,您可能不知道),否则我将避免支持它们并保留您的当前代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM