无法识别js文件内的函数
calculateGPA.html如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Web page/Contact</title>
<link rel="stylesheet" href="common.css">
<link rel="icon" href="pictures/favicon.ico" type="image/x-icon">
<script type="text/javascript" src="generalFunction.js">
</script>
<script>
window.onload=function(){
var submit=document.getElementById("submit");
submit.onclick=function(){
event.preventDefault();
request("gpa.js");
console.log("gpa.js has onloaded.");
}
be_gray();
}
</script>
</head>
<body>
<div id="wrapper">
<header>
<h1>Beijing Jiaotong University</h1>
</header>
<nav>
<ul style="margin: 0;" id="navigation">
<li><a href="home.html">Home</a>
</li>
<li><a href="Services.html" target="_blank">Services</a>
<ul>
<li><a href="CalculateGPA.html">calculate</a></li>
</ul>
</li>
<li><a href="Contact.html" target="_blank">Contact</a>
</li>
</ul>
</nav>
<main>
<br>
<div id="formContainer"></div>
<div id="calculate"></div>
<form id="myForm">
<label for="number">the number of curriculums you want to calculate:</label>
<input type="number" name="number" id="number" required>
<button type="submit" id="submit" value="submit">submit</button>
</form>
<div id="result"></div>
</main>
<footer>
<hr>
<p id="decoration"><a href="http://jigsaw.w3.org/css-validator/check/referer">
<img style="border:0;width:88px;height:31px"
src="http://jigsaw.w3.org/css-validator/images/vcss"
alt="Valid CSS!" />
</a>
<a href="http://jigsaw.w3.org/css-validator/check/referer">
<img style="border:0;width:88px;height:31px"
src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
alt="Valid CSS!" />
</a>
</p>
<p>Thanks for <a href="http://webdevbasics.net">Basics of Web Design Textbook Companian</a></p>
<p id="right">Copyright © 2024 LiRuize.All right reserved</p>
</footer>
</div>
</body>
</html>
其中request函数在generalFunction.js中,代码如下
/*上传js脚本*/
export function request(url){
var script=document.createElement("script");
script.type="text/javascript";
if(script.readyState){
script.onreadyStatechange=function(){
if(script.readyState=="complete"||script.readyState=="loaded"){
script.onreadyStatechange=null;
}
};
console.log("IE");
}else{
script.onload=function(){
};
}
script.src=url;
document.getElementsByTagName("head")[0].appendChild(script);
}
console.log("generalFunction.js has onloaded.");
gpa.js如下
console.log("generate table has been submit.");
var number = parseInt(document.getElementById("number").value);
for (var i = 0; i < number; i++) {
var fieldset = document.createElement("fieldset");
var legend = document.createElement("legend");
legend.appendChild(document.createTextNode(i+1+" curriculum"));
fieldset.appendChild(legend);
var label1 = document.createElement("label");
label1.htmlFor = "score" + i;
label1.appendChild(document.createTextNode("Score:"));
fieldset.appendChild(label1);
var input1 = document.createElement("input");
input1.type = "number";
input1.id = "score" + i;
input1.className = "score";
input1.required = true;
input1.min = 0;
input1.max = 100;
fieldset.appendChild(input1);
var label2 = document.createElement("label");
label2.htmlFor = "credit" + i;
label2.appendChild(document.createTextNode("Credit:"));
fieldset.appendChild(label2);
var input2 = document.createElement("input");
input2.type = "number";
input2.id = "credit" + i;
input2.className = "credit";
input2.required = true;
input2.min = 0;
input2.max = 20;
fieldset.appendChild(input2);
document.getElementById("formContainer").appendChild(fieldset);
}
console.log("table has been generated.");
be_gray();
var gpaForm=document.getElementById("myForm");
empty(gpaForm);
var calculateBtn=document.createElement("input");
var startCal=document.getElementById("calculate");
calculateBtn.type="button";
calculateBtn.value="CalculateGPA";
startCal.appendChild(calculateBtn);
calculateBtn.onclick=function(){
var scoreAll=new Array(number);
var valueAll=new Array(number);
var every=new Array(number);
var res=0;
var values=0;
for(var i=0;i<number;i++){
scoreAll[i]= parseInt(document.getElementById("score"+i).value);
valueAll[i]=parseInt(document.getElementById("credit"+i).value);
values+=valueAll[i];
}
for(var i=0;i<number;i++){
every[i]=scoreAll[i]*(valueAll[i]/values);
res+=every[i];
}
res/=25;
document.getElementById("result").appendChild(document.createTextNode("Your GPA is "+res.toFixed(2)));
}