这里是我的代码:多个JavaScript功能工作不正常
<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按钮也会停止工作。为什么是这样?
确保你提问之前请检查错误控制台... – Shmiddty