这是我的代码:
<script type="text/javascript" language="javascript">
function calc(){
var a = prompt("Enter a number");
var b = prompt("Enter another number");
var x = +a;
var y = +b;
alert("The answer is " + (x+y));
}
function rect(type){
var a = prompt("Enter length of rectangle");
var b = prompt("Enter width of rectangle");
var x = +a;
var b = +b;
if(type=="area"){
alert("The area is " + (x*y);
}
else if(type=="perimeter"){
alert("The perimeter is " + ( (2*x)+(2*y) );
}
}
</script>
<head>
<title>JavaScript Calculator</title>
</head>
<body>
<button onclick="calc()">Calculator</button>
<button onclick="rect('area')">Area of Rectangle</button>
<button onclick="rect('perimeter')">Perimeter of Rectangle</button>
</body>
当我注释掉rect函数时,我的代码运行良好,只是“计算器”按钮是唯一起作用的按钮。其他两个按钮应该可以使用rect函数正常工作,但是不能正常工作,并且当我取消对rect函数的注释时,Calculator按钮也将停止工作。为什么是这样?
最佳答案
您缺少一些括号
alert("The area is " + (x*y));
// and:
alert("The perimeter is " + ( (2*x)+(2*y) ));
关于javascript - 多个JavaScript函数无法正常运行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14045697/