形式? [英] Defining forms?

查看:93
本文介绍了形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是JavaScript的新手,我的第一个任务是创建一个简单的

游戏。下面是我的代码在IE中工作正常,但在其他任何浏览器中都没有。

(Opera,Netscape,Mozilla)。它没有定义任何形式但它使用表单按钮。

就像我说的,它在IE中工作正常,但是当加载到另一个浏览器和

开始游戏时!单击按钮,似乎没有任何事情发生。没有

错误消息出现。任何帮助,将不胜感激。谢谢。

-

要联系我,请使用andrewbroomhead {AT} yahoo {DOT} co {DOT}英国


我从一开始就删除了一些毫无意义的HTML代码


< script type =" text / JavaScript">

<! - < br $>
var GamePics = new Array(9);

var i;

var j;

var ind; < br $>
var AccClick = 0;

var PlayingGame = 0;

GamePics [0] = new Image();

GamePics [0] .src =" images / 1.gif" ;;

GamePics [1] = new Image();

GamePics [1] .src = images / 2.gif;

GamePics [2] =新图像();

GamePics [2] .src =" images / 3.gif" ;

GamePics [3] =新图片();

GamePics [3] .src =" images / 4.gif";

GamePics [4] =新图像();

GamePics [4] .src =" images / 5.gif";

GamePics [5] =新图片();

GamePics [5] .src =" images / 6.gif";

GamePics [6] = new Image( );

GamePics [6] .src =" images / 7.gif" ;;

GamePics [7] = new Image();

GamePics [7] .src =" images / 8.gif";

GamePics [8] = new Image();

GamePics [8] .src =" images / Blank.gif";

功能点击(NumClicked)

{

if(AccClick == 0)

alert("请点击开始游戏!开始);

其他

{

if(NumClicked == 1&& image [1] .src == GamePics [8] .src)

交换(0,1);

if(NumClicked == 1&& image [3] .src == GamePics [8 ] .src)

交换(0,3);

if(NumClicked == 2&& image [0] .src == GamePics [8]。 src)

交换(1,0);

if(NumClicked == 2&& image [2] .src == GamePics [8] .src)

交换(1,2);

if(NumClicked == 2&& image [4] .src == GamePics [8] .src)

交换(1,4);

if(NumClicked == 3&& image [1] .src == GamePics [8] .src)

交换(2,1);

if(NumClicked == 3&& image [5] .src == GamePics [8] .src)

交换(2,5);

if(NumClicked == 4&& image [0] .src == GamePics [8] .src)

交换(3,0);

if(NumClicked == 4&& image [4] .src == GamePics [8] .src)

交换( 3,4);

if(NumClicked == 4&& image [6] .src == GamePics [8] .src)

交换(3,6);

if(NumClicked == 5&& image [3] .src == GamePics [8] .src)

交换(4,3);

if(NumClicked == 5&& image [ 1] .src == GamePics [8] .src)

交换(4,1);

if(NumClicked == 5&& image [5] .src == GamePics [8] .src)

交换(4,5);

if(NumClicked == 5&& image [7] .src == GamePics [8] .src)

交换(4,7);

if(NumClicked == 6&& image [2] .src == GamePics [8] .src)

交换(5,2);

if(NumClicked == 6&& image [4] .src == GamePics [ 8] .src)

交换(5,4);

if(NumClicked == 6&& image [8] .src == GamePics [8] .src)

交换(5,8);

if(NumClicked == 7&& image [7] .src == GamePics [8] .src )

交换(6,7);

if(NumClicked == 7&& image [3] .src == GamePics [8] .src)

交换(6,3);

if(NumClicked == 8&& image [6] .src == GamePics [8] .src)

交换(7,6);

if(NumClicked == 8&& image [4] .src == GamePics [8] .sr c)

掉期(7,4);

if(NumClicked == 8&& image [8] .src == GamePics [8] .src)

交换(7,8);

if(NumClicked == 9&& image [ 5] .src == GamePics [8] .src)

交换(8,5);

if(NumClicked == 9&& image [7] .src == GamePics [8] .src)

交换(8,7);

WinCheck();

}

}

函数交换(Pic1,Pic2)

{

spare = new Image();

spare.src = document.image [Pic1] .src;

document.image [Pic1] .src = document.image [Pic2] .src;

document.image [Pic2] .src = spare.src;

}

函数Timer()

{

if(PlayingGame == 1)

{

timeTaken.value ++;

setTimeout(''Timer()'',1000);

}

}

函数Shuffle()

{

if(AccClick == 1)

{

AccClick = 0;

PlayingGame = 0;

startButton.value =" ;开始游戏!" ;;

timeTaken.value = 0;

返回;

}

else

startButton.value ="停止游戏!" ;;

for(j = 0; j< 8; j ++)

{

ind = Math.round(8 * Math.random());

交换(j,ind);

}

AccClick = 1;

timeTaken.value = 0;

PlayingGame = 1;

Timer();

}

函数WinCheck()

{

var NoCorrect = 0;

for(i = 0; i< 8; i ++)

{

if(document.image [i] .src == GamePics [i] .src)

NoCorrect ++;

}

if(NoCorrect == 8)

{

startButton.value = 开始游戏!;

PlayingGame = 0;

AccClick = 0;

alert("祝贺你,你已经完成了游戏!花了

" + timeTaken.value +"秒!");

timeTaken.value = 0;

}

}

// - >

< / script>

< center>< table class =" content">

< tr>

< td class =" innerContent">< table width =" 300"边界=" 0" cellspacing =" 1"

cellpadding =" 1"

height =" 300">

< tr>

< td>< A border =''0''href =" javascript :点击(''1'')">< img border = ''0''

name =" image" src =" images / 1.gif">< / A>< / td>

< td>< A border =''0''href =" javascript < b> :点击(''2'')">< img border =''0''

name =" image" src =" images / 2.gif">< / A>< / td>

< td>< A border =''0''href =" javascript < b> :点击(''3'')">< img border =''0''

name =" image" src =" images / 3.gif">< / A>< / td>

< / tr>

< tr>

< td>< A border =''0''href =" javascript :点击(''4'')">< img border =' '0''

name =" image" src =" images / 4.gif">< / A>< / td>

< td>< A border =''0''href =" javascript < b> :点击(''5'')">< img border =''0''

name =" image" src =" images / 5.gif">< / A>< / td>

< td>< A border =''0''href =" javascript < b> :点击(''6'')">< img border =''0''

name =" image" src =" images / 6.gif">< / A>< / td>

< / tr>

< tr>

< td>< A border =''0''href =" javascript :点击(''7'')">< img border =' '0''

name =" image" src =" images / 7.gif">< / A>< / td>

< td>< A border =''0''href =" javascript < b> :点击(''8'')">< img border =''0''

name =" image" src =" images / 8.gif">< / A>< / td>

< td>< A border =''0''href =" javascript < b> :点击(''9'')">< img border =''0''

name =" image" src =" images / Blank.gif">< / A>< / td>

< / tr>

< / table>

< p>

< input type =''button''type =''button''name =''startButton''value =''开始
Game!''OnMouseDown =''javascript :随机播放()''>

< font color =" #FFFFFF"大小= QUOT; 3英寸face =" Geneva,Arial,Helvetica,

sans-serif">

你已经采取了& nbsp;

<输入类=''box''type =''text''name =''timeTaken''readonly size ='''''

value =''0''>

秒< / font>< br>

< / p>

< p>& nbsp; < / p>

< table width =" 100%" border =" 0">

< tr>

< td width =" 72%">< font color =" #FFFFFF" ;大小= QUOT; 3英寸face =" Verdana,Arial,

Helvetica,

sans-serif">

完成的图片如下所示:< / font>< / td>

< td width =" 28%">< font face =" Verdana,Arial,Helvetica,

sans -serif">< img

src =" Images / Complete.gif"宽度= QUOT; 200"高度= QUOT; 200" border =" 2">< / font>< / td>

< / tr>

< / table>

< p>& nbsp;< / p>< / body>

< / html>

解决方案

2004年2月10日星期二18:04:34 -0000,Andy B< No Spam Thanks>写道:

我是JavaScript的新手,我的第一个任务是创建一个简单的游戏。下面是我的代码在IE中运行良好,但在任何其他浏览器(Opera,Netscape,Mozilla)中都没有。它没有定义任何形式它使用
表单按钮。
就像我说的,它在IE中工作正常,但加载到另一个浏览器和开始游戏!单击按钮,似乎没有任何事情发生。没有
错误消息。任何帮助,将不胜感激。谢谢。


我会引用您的代码部分,然后评论我看到的任何问题。一些

的评论只是一般指导原则,实际上并没有解决你的问题。但是,它们仍应给予一些考虑。

< A border =''0''href =" javascript :点击(''1'')" ;>< img border =''0''
name =" image" SRC ="图像/ 1.gif">< / A>


1)不要使用JavaScript伪协议(javascript :)而没有良好的

原因。您可以使用以下任一方式执行上述操作:


< a href =""的onclick = QUOT;点击( 1’ ); return false;">< img src =" images / 1.gif"

alt ="简短说明">< / a>


或:


< img src =" images / 1.gif" alt ="简短说明" onclick ="点击(''1'')">


请注意,前者应该用CSS设置样式,后者不能工作

with NN4。


2)你在上面的电话中传递了字符串''1',但你检查了

功能中的数字:


功能点击(NumClicked){

...

if(NumClicked == 1& & image [1] .src == GamePics [8] .src)

交换(0,1);


选择其中一个。


if(NumClicked == 1&& image [1] .src == GamePics [8] .src)
交换(0,1);


您使用上面的''image'作为全局变量。但是,它不是 - 它是几个HTML元素的

名称。这是主要问题所在。您可以通过document.images集合访问图像。这可以通过索引(整个文档中的第一个图像为0)或按名称来完成

。如果你使用后者,你必须给每个图像一个唯一的名称。

< input type =''button''type =''button ''name =''startButton''value =''开始
游戏!''OnMouseDown =''javascript :Shuffle()''>


你可能想要使用onclick内在事件,而不是onmousedown。

此外,在前面加上javascript :一个内在事件不会在IE以外的任何浏览器中指定语言。实现这一目标的正确方法是在HEAD部分使用META元素:


< meta http-equiv =" Content-Script-类型" content =" text / javascript">

< script type =" text / JavaScript">
<! -




您不需要使用SGML注释附上脚本内容。这样做的原因不再有效。另外,按照惯例,MIME类型

是用小写字母写的,但是没有任何区别(类型

属性不区分大小写)。


迈克


将来,请不要在签名旁边放置任何除签名以外的任何东西

签名标记(" ; - ")。


-

Michael Winter
M。****** @ blueyonder.co.inva lid(将.invalid替换为.uk以回复)


Andy B说:



我是JavaScript的新手,我的第一个任务就是创建一个简单的游戏。下面是我的代码,它可以在IE中正常工作,但不能在任何其他浏览器中使用
(Opera,Netscape,Mozilla)。它没有定义任何形式但它使用表单按钮。
就像我说的,它在IE中工作正常,但加载到另一个浏览器和开始游戏!单击按钮,似乎没有任何事情发生。没有
错误消息。任何帮助,将不胜感激。谢谢。
-
要联系我,请使用andrewbroomhead {AT} yahoo {DOT} co {DOT} uk




这是USENET 。

要与您联系,我们会回复您在新闻组中的帖子。


如果您没有在Netscape中看到错误消息,那么您''不要在正确的地方寻找



在Netscape 7中,使用下拉菜单项:


工具> Web开发> JavaScript控制台


您应该看到一条消息,抱怨未定义startButton。

大多数浏览器都不允许您通过引用元素来逃避/>
只是通过他们的名字,没有任何上下文信息。


另请注意,您选择随机数的方法并不是真的

以你(也可能是你的教练)认为它的方式工作:

ind = Math.round(8 * Math.random());

选择0和8半和任何其他价值观一样频繁。




" Michael Winter" < M ****** @ blueyonder.co.invalid>。在消息中写道

news:op ************** @ news-text.blueyonder.co.uk ...

On星期二,2004年2月10日18:04:34 -0000,Andy B< No Spam Thanks>写道:

我是JavaScript的新手,我的第一个任务是创建一个
的简单游戏。下面是我的代码在IE中运行良好,但在任何其他浏览器(Opera,Netscape,Mozilla)中都没有。它没有定义任何形式它使用
表单按钮。
就像我说的,它在IE中工作正常,但加载到另一个浏览器
和开始游戏!单击按钮,似乎没有任何事情发生。没有
错误消息。任何帮助,将不胜感激。谢谢。
我会引用您的代码部分,然后评论我看到的任何问题。一些评论只是一般指导原则,实际上并不能解决你的问题。但是,它们仍应给予一些考虑。

< A border =''0''href =" javascript :点击(''1 '')">< img border =''0''
name =" image" src =" images / 1.gif">< / A>



1)不要使用JavaScript伪协议(javascript :)没有一个好的原因。您可以使用以下任一方式执行上述操作:

< a href =""的onclick = QUOT;点击( 1’ ); return false;">< img



src =" images / 1.gif" alt ="简短描述">< / a>

或:

< img src =" images / 1.gif" alt ="简短说明" onclick =" Clicked(''1')">

请注意,前者应使用CSS设置样式,后者不能与NN4一起使用。

2)你在上面的调用中传递了字符串''1',但你检查了函数中的
数字:

功能点击( NumClicked){
...
if(NumClicked == 1&& image [1] .src == GamePics [8] .src)
交换(0,1);

选择其中一个。

if(NumClicked == 1&& image [1] .src == GamePics [ 8] .src)
交换(0,1);



上面使用''image''作为全局变量。但是,它不是 - 它是几个HTML元素的名称。这是主要问题所在。您可以通过document.images集合访问图像。这可以通过索引(整个文档中的第一个图像为0)或按名称来完成。如果你使用后者,你必须给每个图像一个唯一的名称。

< input type =''button''type =''button ''name =''startButton''value =''开始
游戏!''OnMouseDown =''javascript :随机播放()''>



你可能想要使用onclick内部事件,而不是onmousedown。
此外,在前面加上javascript :内部事件不会在IE以外的任何浏览器中指定语言。实现这一点的正确方法是在HEAD部分内使用META元素:

< meta http-equiv =" Content-Script-Type" content =" text / javascript">

< script type =" text / JavaScript">
<! -



您不需要使用SGML注释附上脚本内容。这样做的原因不再有效。此外,按照惯例,MIME类型
是用小写字母写的,但不应有任何区别(类型
属性不区分大小写)。

迈克
将来,除了签名外,请不要放置签名标记( - )下面的任何内容。

- Michael Winter
M.******@blueyonder.co.inva 盖子(将.invalid替换为.uk以回复)




干杯们,现在修好一切,一切都好。

非常感谢你的帮助。

Andy


Hi,
I am very new to JavaScript and my first assignment is to create a simple
game. Below is my code which works fine in IE, but not in any other browser
(Opera, Netscape, Mozilla). It has no form defined yet it uses form buttons.
Like I said, it works fine in IE but when loaded into another browser and
the Start Game! button is clicked, nothing seems to happen. There are no
error messages apperaing. Any help would be appreciated. Thanks.
--
To contact me, please use andrewbroomhead {AT} yahoo {DOT} co {DOT} uk

I have deleted some pointless HTML code from the beginning

<script type="text/JavaScript">
<!--
var GamePics = new Array(9);
var i;
var j;
var ind;
var AccClick=0;
var PlayingGame=0;
GamePics[0] = new Image();
GamePics[0].src="images/1.gif";
GamePics[1] = new Image();
GamePics[1].src="images/2.gif";
GamePics[2] = new Image();
GamePics[2].src="images/3.gif";
GamePics[3] = new Image();
GamePics[3].src="images/4.gif";
GamePics[4] = new Image();
GamePics[4].src="images/5.gif";
GamePics[5] = new Image();
GamePics[5].src="images/6.gif";
GamePics[6] = new Image();
GamePics[6].src="images/7.gif";
GamePics[7] = new Image();
GamePics[7].src="images/8.gif";
GamePics[8] = new Image();
GamePics[8].src="images/Blank.gif";
function Clicked(NumClicked)
{
if (AccClick == 0)
alert ("Please click on Start Game! to begin");
else
{
if (NumClicked == 1 && image[1].src == GamePics[8].src)
Swap(0,1);
if (NumClicked == 1 && image[3].src == GamePics[8].src)
Swap(0,3);
if (NumClicked == 2 && image[0].src == GamePics[8].src)
Swap(1,0);
if (NumClicked == 2 && image[2].src == GamePics[8].src)
Swap(1,2);
if (NumClicked == 2 && image[4].src == GamePics[8].src)
Swap(1,4);
if (NumClicked == 3 && image[1].src == GamePics[8].src)
Swap(2,1);
if (NumClicked == 3 && image[5].src == GamePics[8].src)
Swap(2,5);
if (NumClicked == 4 && image[0].src == GamePics[8].src)
Swap(3,0);
if (NumClicked == 4 && image[4].src == GamePics[8].src)
Swap(3,4);
if (NumClicked == 4 && image[6].src == GamePics[8].src)
Swap(3,6);
if (NumClicked == 5 && image[3].src == GamePics[8].src)
Swap(4,3);
if (NumClicked == 5 && image[1].src == GamePics[8].src)
Swap(4,1);
if (NumClicked == 5 && image[5].src == GamePics[8].src)
Swap(4,5);
if (NumClicked == 5 && image[7].src == GamePics[8].src)
Swap(4,7);
if (NumClicked == 6 && image[2].src == GamePics[8].src)
Swap(5,2);
if (NumClicked == 6 && image[4].src == GamePics[8].src)
Swap(5,4);
if (NumClicked == 6 && image[8].src == GamePics[8].src)
Swap(5,8);
if (NumClicked == 7 && image[7].src == GamePics[8].src)
Swap(6,7);
if (NumClicked == 7 && image[3].src == GamePics[8].src)
Swap(6,3);
if (NumClicked == 8 && image[6].src == GamePics[8].src)
Swap(7,6);
if (NumClicked == 8 && image[4].src == GamePics[8].src)
Swap(7,4);
if (NumClicked == 8 && image[8].src == GamePics[8].src)
Swap(7,8);
if (NumClicked == 9 && image[5].src == GamePics[8].src)
Swap(8,5);
if (NumClicked == 9 && image[7].src == GamePics[8].src)
Swap(8,7);
WinCheck();
}
}
function Swap(Pic1, Pic2)
{
spare=new Image();
spare.src=document.image[Pic1].src;
document.image[Pic1].src=document.image[Pic2].src;
document.image[Pic2].src=spare.src;
}
function Timer()
{
if(PlayingGame==1)
{
timeTaken.value++;
setTimeout(''Timer()'', 1000);
}
}
function Shuffle()
{
if(AccClick==1)
{
AccClick=0;
PlayingGame=0;
startButton.value="Start Game!";
timeTaken.value=0;
return;
}
else
startButton.value="Stop Game!";
for (j=0;j<8;j++)
{
ind=Math.round(8*Math.random());
Swap(j,ind);
}
AccClick = 1;
timeTaken.value=0;
PlayingGame=1;
Timer();
}
function WinCheck()
{
var NoCorrect=0;
for (i=0;i<8;i++)
{
if (document.image[i].src == GamePics[i].src)
NoCorrect++;
}
if (NoCorrect == 8)
{
startButton.value="Start Game!";
PlayingGame=0;
AccClick=0;
alert("Congratulations, You have completed the game! It took
"+timeTaken.value+" seconds!");
timeTaken.value=0;
}
}
//-->
</script>
<center><table class="content">
<tr>
<td class="innerContent"><table width="300" border="0" cellspacing="1"
cellpadding="1"
height="300">
<tr>
<td><A border=''0'' href="javascript:Clicked(''1'')"><img border=''0''
name="image" src="images/1.gif"></A></td>
<td><A border=''0'' href="javascript:Clicked(''2'')"><img border=''0''
name="image" src="images/2.gif"></A></td>
<td><A border=''0'' href="javascript:Clicked(''3'')"><img border=''0''
name="image" src="images/3.gif"></A></td>
</tr>
<tr>
<td><A border=''0'' href="javascript:Clicked(''4'')"><img border=''0''
name="image" src="images/4.gif"></A></td>
<td><A border=''0'' href="javascript:Clicked(''5'')"><img border=''0''
name="image" src="images/5.gif"></A></td>
<td><A border=''0'' href="javascript:Clicked(''6'')"><img border=''0''
name="image" src="images/6.gif"></A></td>
</tr>
<tr>
<td><A border=''0'' href="javascript:Clicked(''7'')"><img border=''0''
name="image" src="images/7.gif"></A></td>
<td><A border=''0'' href="javascript:Clicked(''8'')"><img border=''0''
name="image" src="images/8.gif"></A></td>
<td><A border=''0'' href="javascript:Clicked(''9'')"><img border=''0''
name="image" src="images/Blank.gif"></A></td>
</tr>
</table>
<p>
<input type=''button'' type=''button'' name=''startButton'' value=''Start
Game!'' OnMouseDown=''javascript:Shuffle()''>
<font color="#FFFFFF" size="3" face="Geneva, Arial, Helvetica,
sans-serif">
You have taken&nbsp;
<input class=''box'' type=''text'' name=''timeTaken'' readonly size=''2''
value=''0''>
seconds</font><br>
</p>
<p>&nbsp; </p>
<table width="100%" border="0">
<tr>
<td width="72%"><font color="#FFFFFF" size="3" face="Verdana, Arial,
Helvetica,
sans-serif">The
completed picture looks like this: </font></td>
<td width="28%"><font face="Verdana, Arial, Helvetica,
sans-serif"><img
src="Images/Complete.gif" width="200" height="200" border="2"></font></td>
</tr>
</table>
<p>&nbsp;</p></body>
</html>

解决方案

On Tue, 10 Feb 2004 18:04:34 -0000, Andy B <No Spam Thanks> wrote:

I am very new to JavaScript and my first assignment is to create a simple
game. Below is my code which works fine in IE, but not in any other
browser (Opera, Netscape, Mozilla). It has no form defined yet it uses
form buttons.
Like I said, it works fine in IE but when loaded into another browser and
the Start Game! button is clicked, nothing seems to happen. There are no
error messages apperaing. Any help would be appreciated. Thanks.
I''ll quote sections of your code, then comment on any issues I see. Some
of the comments will be general guidelines only, and won''t actually solve
your problem. However, they should still be given some consideration.
<A border=''0'' href="javascript:Clicked(''1'')"><img border=''0''
name="image" src="images/1.gif"></A>
1) Don''t use the JavaScript pseudo-protocol (javascript:) without a good
reason. You can do the above with either of:

<a href="" onclick="Clicked(''1''); return false;"><img src="images/1.gif"
alt="short description"></a>

or:

<img src="images/1.gif" alt="short description" onclick="Clicked(''1'')">

Note that the former should be styled with CSS, and the latter won''t work
with NN4.

2) You pass the string, ''1'', in the call above, but you check against a
number in the function:

function Clicked(NumClicked) {
...
if (NumClicked == 1 && image[1].src == GamePics[8].src)
Swap(0,1);

Choose one or the other.

if (NumClicked == 1 && image[1].src == GamePics[8].src)
Swap(0,1);
You use ''image'' above as a global variable. However, it is not - it is the
name of several HTML elements. This is where the main issue lies. You can
access the images through the document.images collection. This can be done
either by index (first image in the entire document is 0), or by name. If
you use the latter, you''ll have to give each image a unique name.
<input type=''button'' type=''button'' name=''startButton'' value=''Start
Game!'' OnMouseDown=''javascript:Shuffle()''>
You probably want to use the onclick intrinsic event, not onmousedown.
Furthermore, prepending "javascript:" to an intrinsic event does not
specify the language in any browser other than IE. The proper way to
acheive this is to use a META element inside the HEAD section:

<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/JavaScript">
<!--



You don''t need to enclose script content with SGML comments. The archaic
reasons for doing so are no longer valid. Also, by convention, MIME types
are written in lowercase, but there shouldn''t be any difference (the type
attribute is case insensitive).

Mike

In future, please don''t place anything other than your signature, below
the signature marker ("-- ").

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)


Andy B said:


Hi,
I am very new to JavaScript and my first assignment is to create a simple
game. Below is my code which works fine in IE, but not in any other browser
(Opera, Netscape, Mozilla). It has no form defined yet it uses form buttons.
Like I said, it works fine in IE but when loaded into another browser and
the Start Game! button is clicked, nothing seems to happen. There are no
error messages apperaing. Any help would be appreciated. Thanks.
--
To contact me, please use andrewbroomhead {AT} yahoo {DOT} co {DOT} uk



This is USENET.
To contact you, we respond to your post in the newsgroup.

If you''re not seeing error messages in Netscape, then you''re not looking
in the right place.

In Netscape 7, use the pulldown menu item:

Tools > Web Development > JavaScript Console

You should see a message complaining that startButton is not defined.
Most browsers don''t let you get away with referring to elements
simply by their names, without any context information.

Also note that your method of choosing random numbers doesn''t really
work the way you (and possibly your instructor) think it does:
ind=Math.round(8*Math.random());
chooses 0 and 8 half as often as any of the other values.



"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...

On Tue, 10 Feb 2004 18:04:34 -0000, Andy B <No Spam Thanks> wrote:

I am very new to JavaScript and my first assignment is to create a simple game. Below is my code which works fine in IE, but not in any other
browser (Opera, Netscape, Mozilla). It has no form defined yet it uses
form buttons.
Like I said, it works fine in IE but when loaded into another browser and the Start Game! button is clicked, nothing seems to happen. There are no
error messages apperaing. Any help would be appreciated. Thanks.
I''ll quote sections of your code, then comment on any issues I see. Some
of the comments will be general guidelines only, and won''t actually solve
your problem. However, they should still be given some consideration.

<A border=''0'' href="javascript:Clicked(''1'')"><img border=''0''
name="image" src="images/1.gif"></A>



1) Don''t use the JavaScript pseudo-protocol (javascript:) without a good
reason. You can do the above with either of:

<a href="" onclick="Clicked(''1''); return false;"><img


src="images/1.gif" alt="short description"></a>

or:

<img src="images/1.gif" alt="short description" onclick="Clicked(''1'')">

Note that the former should be styled with CSS, and the latter won''t work
with NN4.

2) You pass the string, ''1'', in the call above, but you check against a
number in the function:

function Clicked(NumClicked) {
...
if (NumClicked == 1 && image[1].src == GamePics[8].src)
Swap(0,1);

Choose one or the other.

if (NumClicked == 1 && image[1].src == GamePics[8].src)
Swap(0,1);



You use ''image'' above as a global variable. However, it is not - it is the
name of several HTML elements. This is where the main issue lies. You can
access the images through the document.images collection. This can be done
either by index (first image in the entire document is 0), or by name. If
you use the latter, you''ll have to give each image a unique name.

<input type=''button'' type=''button'' name=''startButton'' value=''Start
Game!'' OnMouseDown=''javascript:Shuffle()''>



You probably want to use the onclick intrinsic event, not onmousedown.
Furthermore, prepending "javascript:" to an intrinsic event does not
specify the language in any browser other than IE. The proper way to
acheive this is to use a META element inside the HEAD section:

<meta http-equiv="Content-Script-Type" content="text/javascript">

<script type="text/JavaScript">
<!--



You don''t need to enclose script content with SGML comments. The archaic
reasons for doing so are no longer valid. Also, by convention, MIME types
are written in lowercase, but there shouldn''t be any difference (the type
attribute is case insensitive).

Mike

In future, please don''t place anything other than your signature, below
the signature marker ("-- ").

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)



Cheers guys, fixed it now and everything is ok.
Thanks very much for the help.
Andy


这篇关于形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆