gpt4 book ai didi

javascript - 如何写一个不死边界的变量

转载 作者:行者123 更新时间:2023-11-28 08:22:36 25 4
gpt4 key购买 nike

我是 JS 的新手,我正在尝试进行简单的体重计算。它需要用户的高度和体重进行计算并将其显示在屏幕上。我已经做到了。现在我的问题是我想在“清除信息”按钮下将结果显示为文本。我想使用 DOM 来做到这一点。

请帮忙!

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Assignment 4 Starter File</title>

<style>
h1 {text-align: center;}

#calculator
{
width: 600px;
margin: 0px auto;
border: 2px solid #000;
padding: 20px;
}

#values
{
width: 600px;
margin: 50px auto;
pading: 20px;
}

ul {margin-top: 5px;}


</style>

<script>


// code to be called when button is clicked goes here
function calculateResult() {

// this gets the weight
var weight = document.getElementById("weight").value;
//this gets the height
var height = document.getElementById("height").value;
// this calcualtes thet total with a
var calculate = (weight / Math.pow(height,2)) * 703;
//rounds to the 2
var New = calculate.toFixed(2);
New = parseInt(New);
alert(New);
// alert(" your weight is" + weight);

if(calculate <=18){
("your BMI IS: " + calculate );
// alert("your less then 18");

}




}

window.onload = function() {

// reference the button to be clicked
var btnCalculate = document.getElementById("calculate");


// calls the function when the button is clicked
btnCalculate.onclick = calculateResult;

}
</script>



</head>

<body>

<header>
<h1>Body Mass Index (BMI) Calculator</h1>
</header>



<section id="calculator">

<p>
To determine if you're at a healthy weight, enter the following information
and calculate your body mass index (BMI). <span>Enter numbers only please</span>
</p>

<form>

<label for="weight" >Weight (lbs):</label>
<br>
<input type="text" id="weight">
<br>
<br>

<label for="height" >Height (inches):</label>
<br>
<input type="text" id="height">
<br>
<br>

<input type="button" id="calculate" value="Click to Calculate Your BMI"> <br>
<br>
<input type="reset" value="Clear the Information">
<p style="color: red">The total is <span id= "total"> </span></p></p>


</section>


<section id="values">



BMI values are as follows:
<ul>
<li>Below 18: Underweight</li>
<li>Between 18 and 24.9 : Normal</li>
<li>Between 25 and 29.9 : Overweight</li>
<li>Over 30 : Obese</li>
</ul>

</p>

</section>

<script type="text/javascript">



</script>

</body>
</html>

最佳答案

为了实现这一点,您可以使用 innerHTML 属性。这将返回或设置元素的 HTML 内容。

所以我要做的是将 alert(New); 切换为 document.getElementById("total").innerHTML = New; 在你的 计算结果()函数。

这会将 id == "total"的 span 元素的 HTML 内容更改为变量 "New"的值。

所以它看起来像这样。

function calculateResult() {

// this gets the weight
var weight = document.getElementById("weight").value;
//this gets the height
var height = document.getElementById("height").value;
// this calcualtes thet total with a
var calculate = (weight / Math.pow(height,2)) * 703;
//rounds to the 2
var New = calculate.toFixed(2);
New = parseInt(New);

document.getElementById("total").innerHTML = New;

JSFiddle

关于javascript - 如何写一个不死边界的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28685492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com