再来一次!为什么失败? [英] starting again! why fails?

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

问题描述

您好,


以下代码适用于


document.getElementById(''picture'')。src = window [" ; picture"] [0] .src;


但不是如果我将索引增加到1,即


document.getElementById(' 'picture'')。src = window [" picture"] [1] .src;


这是为什么?!


Geoff

<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN">


< html>

< HEAD>


< link rel = stylesheet href =" slider.css" type =" text / css">


< script type =" text / javascript">


var picture = new Array();

var ig = 0;

var ig_max = 7;


函数preload_imgs()

{

if(ig< ig_max)

{

picture [ig] = new Image();

picture [ig] .onload = preload_imgs;

picture [ig] .src =" pic" + ig +" .jpg";

ig ++;

}

}


preload_imgs ();


函数fillTable()

{

document.getElementById(''picture'')。src = window [" picture"] [0] .src;

}


< / script>

< / head> ;


< body onload =" fillTable()">

< table>

< tr> ;

< td>< img ID =''picture''src ="" /> loading< / td>

< / tr>

< / table>

< / body>

< / html>

Hello,

The following code works with

document.getElementById(''picture'').src = window["picture"][0].src;

but not if I increase the index to 1, ie

document.getElementById(''picture'').src = window["picture"][1].src;

Why is this?!

Geoff
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<HEAD>

<link rel=stylesheet href="slider.css" type="text/css">

<script type="text/javascript">

var picture = new Array();
var ig = 0;
var ig_max = 7;

function preload_imgs()
{
if(ig < ig_max)
{
picture[ig] = new Image();
picture[ig].onload = preload_imgs;
picture[ig].src = "pic" + ig + ".jpg";
ig++;
}
}

preload_imgs();

function fillTable()
{
document.getElementById(''picture'').src = window["picture"][0].src;
}

</script>
</head>

<body onload="fillTable()">
<table>
<tr>
<td><img ID=''picture'' src="" />loading</td>
</tr>
</table>
</body>
</html>

推荐答案

Geoff Cox< ge ******* @ notquitecorrectfreeuk。 COM>在消息新闻中写道:hq ******************************** @ 4ax.com ...
Geoff Cox <ge*******@notquitecorrectfreeuk.com> wrote in message news:hq********************************@4ax.com...
您好,

以下代码适用于

document.getElementById(''picture'')。src = window [" picture"] [0]。 src;

但不是如果我将索引增加到1,即

document.getElementById(''picture'')。src = window [" picture"] [ 1] .src;

为什么这个?!

函数preload_imgs()
{
if(ig< ig_max)
{
picture [ig] = new Image();
picture [ig] .onload = preload_imgs;
picture [ig] .src =" pic" + ig +" .jpg";
ig ++;
}
}


您的代码只将一个元素附加到空数组''图片''。将''if''改为''while''。

picture [ig] .onload = preload_imgs;
Hello,

The following code works with

document.getElementById(''picture'').src = window["picture"][0].src;

but not if I increase the index to 1, ie

document.getElementById(''picture'').src = window["picture"][1].src;

Why is this?!

function preload_imgs()
{
if(ig < ig_max)
{
picture[ig] = new Image();
picture[ig].onload = preload_imgs;
picture[ig].src = "pic" + ig + ".jpg";
ig++;
}
}

Your code appends only one element to the empty array ''picture''. Change ''if'' to ''while''.
picture[ig].onload = preload_imgs;




你怎么看?这条线要做什么?


-

SC



What do you consider this line to do?

--
S.C.


2005年9月17日星期六23 :51:02 +0100,Stephen Chalmers

< ig ****** @ lycos.co.uk>写道:
On Sat, 17 Sep 2005 23:51:02 +0100, "Stephen Chalmers"
<ig******@lycos.co.uk> wrote:
function preload_imgs()
{
if(ig< ig_max)
{
picture [ig] = new Image();
picture [ig] .onload = preload_imgs;
picture [ig] .src =" pic" + ig +" .jpg";
ig ++;
}
}
function preload_imgs()
{
if(ig < ig_max)
{
picture[ig] = new Image();
picture[ig].onload = preload_imgs;
picture[ig].src = "pic" + ig + ".jpg";
ig++;
}
}



您的代码只将一个元素附加到空数组''图片''。将''if''改为''while''。



Your code appends only one element to the empty array ''picture''. Change ''if'' to ''while''.

picture [ig] .onload = preload_imgs;
picture[ig].onload = preload_imgs;



你怎么看?这条线要做什么?



What do you consider this line to do?




Stephen,


抱歉错过了你的回复 - 或许你可以看看

之前的线索这个功能如何运作因为这段代码是由Stephane写的,所以我不清楚自己上面这行是做什么的!除了

,他的代码实际上会删除所有图像!


干杯


Geoff



Stephen,

Apologies for missing your reply - perhaps you could look at the
previous threas "how does this function work" as this code is written
by Stephane and I''m not clear myself what the above line does! Except
that his code does actually bring down all the images!

Cheers

Geoff


Geoff Cox说:
Geoff Cox said:

您好,

以下代码适用于

document.getElementById(''picture'')。src = window [" picture"] [0] .src;

但不是如果我将索引增加到1,即

document.getElementById(''picture'')。src = window [" picture"] [1] .src;

为什么会这样?!

Geoff

<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN">

< html>
< HEAD>

< link rel = stylesheet href =" slider.css" type =" text / css">

< script type =" text / javascript">

var picture = new Array();
var ig = 0;
var ig_max = 7;

函数preload_imgs()
{
if(ig< ig_max)
{
picture [ig] = new Image();
picture [ig] .onload = preload_imgs;
picture [ig] .src =" pic" + ig +" .jpg";
ig ++;
}


preload_imgs();

Hello,

The following code works with

document.getElementById(''picture'').src = window["picture"][0].src;

but not if I increase the index to 1, ie

document.getElementById(''picture'').src = window["picture"][1].src;

Why is this?!

Geoff
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<HEAD>

<link rel=stylesheet href="slider.css" type="text/css">

<script type="text/javascript">

var picture = new Array();
var ig = 0;
var ig_max = 7;

function preload_imgs()
{
if(ig < ig_max)
{
picture[ig] = new Image();
picture[ig].onload = preload_imgs;
picture[ig].src = "pic" + ig + ".jpg";
ig++;
}
}

preload_imgs();




这是一种非常糟糕的预先方式。加载图片。首先考虑

首先在

中试图理解它的浪费时间和资源,现在,可以预见,当你试图制作一个

小修改,你必须重新开始询问。


其次,这段代码并不保证图像会是

在页面加载之前加载。这就是为什么我把预先放入在引号中,

,这就是为什么你的代码在页面加载后尝试引用第二张

图像时失败的原因。

你会用一些简单的东西好多了:


< script type =" text / javascript">


var picture = new Array();

for(var ig = 0; ig< 7; ig ++){

picture [ig] = new Image( );

picture [ig] .src =" pic" + ig +" .jpg";

}



That''s a pretty lousy way to "pre" load images. First consider
the time and resources you''ve wasted trying to understand it in
the first place, and now, predictably, when you try to make a
small modification, you have to start asking all over again.

Secondly, this code doesn''t guarantee that the images will be
loaded before the page loads. That''s why I put "pre" in quotes,
and it''s why your code fails when you try to refer to the second
image as soon as the page loads.

You would be much better off with something simple like:

<script type="text/javascript">

var picture = new Array();
for ( var ig=0; ig<7; ig++ ) {
picture[ig] = new Image();
picture[ig].src = "pic" + ig + ".jpg";
}


这篇关于再来一次!为什么失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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