EVAL [英] eval
问题描述
为什么不会做以下工作
for(var i = 0; i< pics; i ++){
eval(''img''+ i)=新图像(wth,hgt)
eval(''img''+ i +''。src'')=''http: //www.mypics/''++++''1.gif''
}
基本上我正在尝试用它们创建一个占位符的数量
相关联的图片!
提前感谢
stuart
>为什么不能做以下工作
for(var i = 0; i< pics; i ++){
eval(''img''+ i)= new图片(wth,hgt)
eval(''img''+ i +''。src'')=''http://www.mypics/''+i+''1.gif''
}
你从哪里得到这个代码?
Douglas Crockford< no **** @ laserlink.net>在留言中写道
news:55 *************************** @ msgid.meganewss ervers.com .. 。为什么不能做以下工作
for(var i = 0; i< pics; i ++){
eval(''img''+ i)=新图像(wth,hgt)
eval(''img''+ i +''。src'')=''http://www.mypics/ ''+ i +''1.gif''
}
你从哪里获得这段代码?
它是我正在编写的更大脚本的一部分,我面临的问题是我无法将src值设置为构造变量
我需要创建一个可变数量的占位符和缓存图像,例如
img1 = new Image(100,200)
img1.src =''someURL' '
img2 =新图片(100,200)
img2.src =''someURL''
img3 = new Image(100,200)
img3.src =''someURL''
我不是rying通过循环构建占位符,如下所示:
for(var i = 1; i< pics; i ++){
(img + i)=新图像(100,200)
(img + i).src =''someURL''
}
我不喜欢什么我不知道如何加入img和我
Stuart写道:
为什么不会做以下工作
因为它是邪恶的[tm] eval(...)。此外,你既不会告诉你什么是b $ b考虑工作,也不会说'pics''aso。是。尽管如此,尽管它的风格很糟糕,但是下面的代码确实创建了Image对象并为它们的`src'属性分配了一个值,所以我觉得它很有用。 (>
for(var i = 0; i< pics; i ++){
eval(''img''+ i)= new Image(wth,hgt)
eval('' img''+ i +''。src'')=''http://www.mypics/''+i+''1.gif''
}
基本上我是尝试使用相关联的图片创建一个占位符的数量!
如果pics存储要创建的Image对象的数量,那么
`wth''和'hgt''是* each *对象的宽度和高度的值,
,要访问的图像资源可通过URL获取
http://www.mypics/X1.gif ,其中X是数字从0到pics - 1,使用
var myImages = new Object();
for(var i = 0; i< pics; i ++)
{
myImages [''img''+ i] = new Image(wth,hgt);
myImages [' 'img''+ i] .src =''http://www.mypics/''+ i +''1.gif'';
}
代替。
PointedEars
why won''t the following work
for(var i=0;i<pics;i++){
eval(''img''+i) = new Image(wth,hgt)
eval(''img''+i+''.src'') = ''http://www.mypics/''+i+''1.gif''
}
basically I am trying to create a numer of placeholders with thier
associated imgages!
thanks in advance
stuart
> why won''t the following work
for(var i=0;i<pics;i++){
eval(''img''+i) = new Image(wth,hgt)
eval(''img''+i+''.src'') = ''http://www.mypics/''+i+''1.gif''
}
Where did you get this code?
Douglas Crockford <no****@laserlink.net> wrote in message
news:55***************************@msgid.meganewss ervers.com...why won''t the following work
for(var i=0;i<pics;i++){
eval(''img''+i) = new Image(wth,hgt)
eval(''img''+i+''.src'') = ''http://www.mypics/''+i+''1.gif''
}
Where did you get this code?
It is part of a bigger script that I am writting, the problem that I am
facing is that I cannot asign the src value to a constucted variable
I need to create a variable amount of placeholders and cache images such as
img1 = new Image(100,200)
img1.src = ''someURL''
img2 = new Image(100,200)
img2.src = ''someURL''
img3 = new Image(100,200)
img3.src = ''someURL''
I am trying to construct the placeholder through a loop as folows
for(var i=1;i<pics;i++){
( img + i ) = new Image(100,200)
(img + i).src = ''someURL''
}
what I don''t know is how to join img and i
Stuart wrote:
why won''t the following work
Because it''s evil[tm] eval(...). Besides, you are neither telling what you
are consider working nor what `pics'' aso. are. Nevertheless, despite its
bad style, the below code does create Image objects and assigns a value to
their `src'' property, so I consider it working.
for(var i=0;i<pics;i++){
eval(''img''+i) = new Image(wth,hgt)
eval(''img''+i+''.src'') = ''http://www.mypics/''+i+''1.gif''
}
basically I am trying to create a numer of placeholders with thier
associated imgages!
Provided that `pics'' stores the number of Image objects to create, that
`wth'' and `hgt'' are values for the width and height of *each* object and
that the image resources to be accessed are available via the URLs
http://www.mypics/X1.gif, where X is a number from 0 to pics - 1, use
var myImages = new Object();
for (var i = 0; i < pics; i++)
{
myImages[''img'' + i] = new Image(wth, hgt);
myImages[''img'' + i].src = ''http://www.mypics/'' + i + ''1.gif'';
}
instead.
PointedEars
这篇关于EVAL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!