我是刚开始学JS,大概7.8天的样子吧
现在我看书貌似我都懂了,可又觉得似乎什么都做不了
高手帮我指点迷途吧
顺便说下我看的书是《javascript教学范本》貌似很浅的一本书这个是我今天下午写的一个华容道的小游戏<html><head><title>华容道</title></head><body onload="chushihua()"><form action="" method="get" name="form1" id="form1">
<input name="pos" id="1" type="button" >
<input name="pos" id="2" type="button">
<input name="pos" id="3" type="button">
<br>
<input name="pos" id="4" type="button">
<input name="pos" id="5" type="button">
<input name="pos" id="6" type="button">
<br>
<input name="pos" id="7" type="button">
<input name="pos" id="8" type="button">
<input name="pos" id="0" type="button">
<br>
<br>
<font id="ci"></font>
<br>
<br>
<input name="game" id="game" type="button" value="重新开始" onclick="newgmae()">
<!--
<input name="game" id="game" type="button" value="测试游戏" onclick="testgame()">
-->
</form>
<hr>
<br>
游戏目标:
<br>
<form>
<input type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
<br>
<input type="button" value="4">
<input type="button" value="5">
<input type="button" value="6">
<br>
<input type="button" value="7">
<input type="button" value="8">
<input type="button" value="0">
</form>
<script language="javascript" type="text/javascript">
var cont;//统计步数
var lw;//0出现的位置
function chushihua() { //为每个pos按钮加入单击事件
for (var i=0 ;i<form1.pos.length ; i++) {
form1.pos.onclick=function ()
{
var thisid=this.id;
var wz=new Array(4);//当前单击按钮的上下左右4个按钮位置
if(this.id==0) thisid=9;//如果pos按钮id为0时表示为第9个按钮,换成9
if (parseInt(thisid)-3>0) {
// 上位置减3如果小于0表示该位置不存在
wz[1]=parseInt(thisid)-3;//上
}else {
wz[1]=0;//不存在时赋值0
}
if ((parseInt(thisid)-1)%3!=0 && parseInt(thisid)-1>0 ) {
//左位置如果整除3表示为上排最后最后一个按钮,如果小于0表示不存在
wz[2]=parseInt(thisid)-1;//左
}
else {
wz[2]=0;//为上排按钮或不存在时赋值0
} if ((parseInt(thisid)+1)%3!=1 && parseInt(thisid)+1<=9 ) {
//右位置如果除3余1表示为下排第一个按钮,如果小于0表示不存在
wz[3]=parseInt(thisid)+1;//右
}
else {
wz[3]=0;//为下排按钮或不存在时赋值0
}
if (parseInt(thisid)+3<=9 ) {
//下位置加3大于9则位置不存在
wz[4]=parseInt(thisid)+3;//下
}
else {
wz[4]=0;//不存在时赋值0
}
if(wz[1]==lw || wz[2]==lw || wz[3]==lw ||wz[4]==lw) { //判断0是否在当前钮的上左右下
form1.pos[lw-1].value=this.value; //和0的按钮交换位置
this.value=0; //和0的按钮交换位置
lw=thisid //设置新的0位置
cont++; //计步数
ci.innerHTML="你已经走了"+cont+"步"
gameover()//检查游戏是否完成
}
}
}
newgmae();
}function gameover() //检查游戏是否完成
{
var wancheng =true;
for (var i=0 ;i<form1.pos.length ;i++ ) {
if(form1.pos.value==form1.pos.id) { //检查所有按钮的标题是否和按钮id相同
wancheng=true;
}else {
wancheng=false; //检查出有按钮标题和按钮id不同,游戏未完成
break;
}
}
if(wancheng == true) {
alert("厉害,恭喜你过关了!");
newgmae();
}
}function testgame() {
for (var i=0 ;i<form1.pos.length ;i++ ) {
form1.pos.value=form1.pos.id;
gameover();
}
}function newgmae() {
var con = new Array(9);
var num ;
for (var i=1 ;i<=9 ;i++ ) {//乱序生成0-8的随机数
var XH=false; //判断循环
while (XH==false) {
num = Math.floor( Math.random()*9);//生成0-8的随机数
if(chashu(num,con)==false) { //检查生成的随机数是否已经出现过
con=num; //如果未出现加入数组
XH=true; //停止循环
form1.pos[i-1].value=con;
if(con==0) lw=i;//记录第一次0的位置
}
}
}
cont=0;
ci.innerHTML="你已经走了0步";
}function chashu(num,con) {//检查生成的数字是否已经出现过
var TorF;//
for (var i=1 ; i<=con.length;i++ ) {
if (num==con) {
TorF=true;//随机数已经出现过,跳出循环,返回true
break;
}
else {
TorF=false;//随机数未出现,继续循环
}
}
return TorF;
}
</script></body></html>
现在我看书貌似我都懂了,可又觉得似乎什么都做不了
高手帮我指点迷途吧
顺便说下我看的书是《javascript教学范本》貌似很浅的一本书这个是我今天下午写的一个华容道的小游戏<html><head><title>华容道</title></head><body onload="chushihua()"><form action="" method="get" name="form1" id="form1">
<input name="pos" id="1" type="button" >
<input name="pos" id="2" type="button">
<input name="pos" id="3" type="button">
<br>
<input name="pos" id="4" type="button">
<input name="pos" id="5" type="button">
<input name="pos" id="6" type="button">
<br>
<input name="pos" id="7" type="button">
<input name="pos" id="8" type="button">
<input name="pos" id="0" type="button">
<br>
<br>
<font id="ci"></font>
<br>
<br>
<input name="game" id="game" type="button" value="重新开始" onclick="newgmae()">
<!--
<input name="game" id="game" type="button" value="测试游戏" onclick="testgame()">
-->
</form>
<hr>
<br>
游戏目标:
<br>
<form>
<input type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
<br>
<input type="button" value="4">
<input type="button" value="5">
<input type="button" value="6">
<br>
<input type="button" value="7">
<input type="button" value="8">
<input type="button" value="0">
</form>
<script language="javascript" type="text/javascript">
var cont;//统计步数
var lw;//0出现的位置
function chushihua() { //为每个pos按钮加入单击事件
for (var i=0 ;i<form1.pos.length ; i++) {
form1.pos.onclick=function ()
{
var thisid=this.id;
var wz=new Array(4);//当前单击按钮的上下左右4个按钮位置
if(this.id==0) thisid=9;//如果pos按钮id为0时表示为第9个按钮,换成9
if (parseInt(thisid)-3>0) {
// 上位置减3如果小于0表示该位置不存在
wz[1]=parseInt(thisid)-3;//上
}else {
wz[1]=0;//不存在时赋值0
}
if ((parseInt(thisid)-1)%3!=0 && parseInt(thisid)-1>0 ) {
//左位置如果整除3表示为上排最后最后一个按钮,如果小于0表示不存在
wz[2]=parseInt(thisid)-1;//左
}
else {
wz[2]=0;//为上排按钮或不存在时赋值0
} if ((parseInt(thisid)+1)%3!=1 && parseInt(thisid)+1<=9 ) {
//右位置如果除3余1表示为下排第一个按钮,如果小于0表示不存在
wz[3]=parseInt(thisid)+1;//右
}
else {
wz[3]=0;//为下排按钮或不存在时赋值0
}
if (parseInt(thisid)+3<=9 ) {
//下位置加3大于9则位置不存在
wz[4]=parseInt(thisid)+3;//下
}
else {
wz[4]=0;//不存在时赋值0
}
if(wz[1]==lw || wz[2]==lw || wz[3]==lw ||wz[4]==lw) { //判断0是否在当前钮的上左右下
form1.pos[lw-1].value=this.value; //和0的按钮交换位置
this.value=0; //和0的按钮交换位置
lw=thisid //设置新的0位置
cont++; //计步数
ci.innerHTML="你已经走了"+cont+"步"
gameover()//检查游戏是否完成
}
}
}
newgmae();
}function gameover() //检查游戏是否完成
{
var wancheng =true;
for (var i=0 ;i<form1.pos.length ;i++ ) {
if(form1.pos.value==form1.pos.id) { //检查所有按钮的标题是否和按钮id相同
wancheng=true;
}else {
wancheng=false; //检查出有按钮标题和按钮id不同,游戏未完成
break;
}
}
if(wancheng == true) {
alert("厉害,恭喜你过关了!");
newgmae();
}
}function testgame() {
for (var i=0 ;i<form1.pos.length ;i++ ) {
form1.pos.value=form1.pos.id;
gameover();
}
}function newgmae() {
var con = new Array(9);
var num ;
for (var i=1 ;i<=9 ;i++ ) {//乱序生成0-8的随机数
var XH=false; //判断循环
while (XH==false) {
num = Math.floor( Math.random()*9);//生成0-8的随机数
if(chashu(num,con)==false) { //检查生成的随机数是否已经出现过
con=num; //如果未出现加入数组
XH=true; //停止循环
form1.pos[i-1].value=con;
if(con==0) lw=i;//记录第一次0的位置
}
}
}
cont=0;
ci.innerHTML="你已经走了0步";
}function chashu(num,con) {//检查生成的数字是否已经出现过
var TorF;//
for (var i=1 ; i<=con.length;i++ ) {
if (num==con) {
TorF=true;//随机数已经出现过,跳出循环,返回true
break;
}
else {
TorF=false;//随机数未出现,继续循环
}
}
return TorF;
}
</script></body></html>
在你认为有问题的语句前面加throw(e);
进行单步调试,一切都清楚了换本书吧,再搜集些好资料