my.html
<html> <head> <script language="javascript" type="text/javascript" src=“myfuns.js"></scritp> <script language="javascript" type="/text/javascript"> var num1 = window.prompt('请输入第一个数:'); var num2 = window.prompt("请输入第2个数:"); var operator = window.prompt("请输入运算符"); num1 = parseFloat(num1); num2 = parseFloat(num2); document.write("res="+jiSuan(num1, num2, operator)); </script> </head> <body> </body> </html>
myfuns.js
//自定义函数,输入两个数,再输入一个运算符,得到结果。 function jiSuan(num1, num2, operator){//强调:参数前,不要加var var res = 0; if (operator == "+"){ res = num1 + num2; }else if (operator == "-"){ res = num1 - num2; }else if (operator == "*"){ res = num1 * num2; }else{ res = num1 / num2; } return res;//返回 }