我想計算平均值並在點擊計算按鈕後向用戶顯示答案。 我想改變名爲Result的頭部,並返回Calculate_Mean函數,它是平均值var,但它不適用於我,我無法確定哪裏出了問題,有什麼幫助?如何用HTML和JavaScript中的函數返回來更改標題文本
function Calculate_Mean() {
var str = document.getElementById("Num").value;
var arr = str.split(",");
var count = arr.length;
var sum;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
var mean = sum/count;
return mean;
}
<header>
<h1> Statistical Website </h1>
<button class="HomeButton" onclick="javascript:window.location.href='HomePage.html'"> Home </button>
</header>
<div id="banner">
<form action="Script URL" method="GET|POST">
<label>
<h1> Enter the data set </h1> </label>
<br>
<textarea name="Num" rows="10" cols="30" autofocus> </textarea>
<br>
<button type="button" onclick="document.getElementById ('Result').value = Calculate_Mean()"> Calculate </button>
<label>
<h1> The result </h1> </label>
<label>
<h1 id="Result"> </h1> </label>
</form>
</div>
我會建議用更多的語義元素替換你正在寫結果的h1元素,比如只讀輸入。 – AndFisher
看到我的回答@黃熙鐳_Gogo – ASK