我希望有一些像10按钮的东西,每次按下它应该减去1.并且到达零它应该消失。 HTML代码
<table width="600" border="0" cellspacing="0" cellpadding="5">
<tr>
<th scope="col"><input type="button" onclick="add()" id='mynum9' value="9" ></th>
<th scope="col"><input type="button" onclick="add()" id='mynum7' value="7" ></th>
<th scope="col"><input type="button" onclick="add()" id='mynum4' value="4" ></th>
<th scope="col"><input type="button" onclick="add()" id='mynum5' value="5" ></th>
<th scope="col"><input type="button" onclick="add()" id='mynum1' value="1" ></th>
</tr>
<tr>
<td><input type="button" onclick="add()" id='mynum3' value="3" ></td>
<td><input type="button" onclick="add()" id='mynum10' value="10" ></td>
<td><input type="button" onclick="add()" id='mynum8' value="8" ></td>
<td><input type="button" onclick="add()" id='mynum2' value="2" ></td>
<td><input type="button" onclick="add()" id='mynum6' value="6" ></td>
</tr>
</table>
function add() {
var numArray=["mynum1". "mynum2","mynum3". "mynum4","mynum5". "mynum6","mynum7". "mynum8","mynum9". "mynum10"]
for (var i = 0; i < numArray.length; i++);
var num = document.getElementById(numArray[i]).value;
if(num == '') num = 0;
if(num==1){
$(this).remove();
}
document.getElementById(numArray[i]).value = parseInt(num ,10) - 1;
}
但它不起作用,我做错了什么。如果你能告诉我最好的JavaScript练习网站
答案 0 :(得分:1)
试试这个:
<table width="600" border="0" cellspacing="0" cellpadding="5">
<tr>
<th scope="col"><input type="button" onclick="add()" id='mynum9' value="9" ></th>
<th scope="col"><input type="button" onclick="add()" id='mynum7' value="7" ></th>
<th scope="col"><input type="button" onclick="add()" id='mynum4' value="4" ></th>
<th scope="col"><input type="button" onclick="add()" id='mynum5' value="5" ></th>
<th scope="col"><input type="button" onclick="add()" id='mynum1' value="1" ></th>
</tr>
<tr>
<td><input type="button" onclick="add()" id='mynum3' value="3" ></td>
<td><input type="button" onclick="add()" id='mynum10' value="10" ></td>
<td><input type="button" onclick="add()" id='mynum8' value="8" ></td>
<td><input type="button" onclick="add()" id='mynum2' value="2" ></td>
<td><input type="button" onclick="add()" id='mynum6' value="6" ></td>
</tr>
</table>
<script>
var numArray=["mynum1", "mynum2","mynum3", "mynum4","mynum5", "mynum6","mynum7", "mynum8","mynum9", "mynum10"];
function add() {
for (var i = 0; i < numArray.length; i++){
var num = document.getElementById(numArray[i]).value;
if(num == 1){
document.getElementById(numArray[i]).remove();
removeA(numArray, numArray[i]);
if(numArray.length > 0){
document.getElementById(numArray[i]).value = parseInt(num ,10);
}
continue;
}
document.getElementById(numArray[i]).value = parseInt(num ,10) - 1;
}
}
function removeA(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
</script>
希望它有所帮助。