我正在嘗試使用JS創建一個計算器webapp,並且遇到了一些問題。我查看了其他開放的線程並嘗試了該代碼,但它似乎沒有工作。我目前只是試圖記錄被按下的按鈕的ID,但是當我按下按鈕時,我得到的是"undefined"
。這裏是我的代碼:獲取剛剛點擊的按鈕的編號
<html>
<head>
<title>Calculator</title>
</head>
<body>
<p id="text">
<table border="0" id="table">
<tr>
<td><input type="button" id="one" value="1" onClick="calculate();"></td>
<td><input type="button" id="two" value="2" onClick="calculate();"></td>
<td><input type="button" id="three" value="3" onClick="calculate();"></td>
</tr>
<tr>
<td><input type="button" id="four" value="4" onClick="calculate();"></td>
<td><input type="button" id="five" value="5" onClick="calculate();"></td>
<td><input type="button" id="six" value="6" onClick="calculate();"></td>
</tr>
<tr>
<td><input type="button" id="seven" value="7" onClick="calculate();"></td>
<td><input type="button" id="eight" value="8" onClick="calculate();"></td>
<td><input type="button" id="nine" value= "9" onClick="calculate();"></td>
</tr>
<tr>
<td> </td>
<td><input type="button" id="zero" value="0" onClick="calculate();"></td>
</tr>
</table>
<script>
function calculate(){
console.log(this.id);
}
</script>
</body>
</html>
[谷歌?](http://stackoverflow.com/questions/4825295/javascript-onclick-get-the-id-of-the-button-clicked) – danielQ 2012-07-12 14:35:36