這裏是我的代碼:多個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