变量作为数组索引 [英] variable as array index

查看:126
本文介绍了变量作为数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的事情如下:

var img_index =" dog";

document.images [img_index] =" blah";


图像[img_index]被视为图像[" img_index"]


我想在图像数组中使用img_index的值。

I want to do something like:
var img_index="dog";
document.images[img_index] = "blah";

images[img_index] is treated like images["img_index"]

I want to use the value of img_index in the images array.

推荐答案

2月24日晚上11点01分,Jammer< ask ... @ mail.comwrote:
On Feb 24, 11:01 pm, Jammer <ask...@mail.comwrote:

我想做类似的事情:

var img_index =" dog";

document.images [img_index] =" blah";


图像[img_index]被视为图像[" img_index"]


我想在images数组中使用img_index的值。
I want to do something like:
var img_index="dog";
document.images[img_index] = "blah";

images[img_index] is treated like images["img_index"]

I want to use the value of img_index in the images array.



使用FF我无法复制您的问题,但您可能想尝试

通过将索引括在()中来强制进行表达式评估'的;即:


document.images [(img_index)]


- Craig Taylor
http://www.ctalkobt.net

Using FF I was unable to duplicate your issue but you may want to try
forcing an expression evaluation by enclosing the index in ()''s; ie:

document.images[(img_index)]

- Craig Taylor
http://www.ctalkobt.net


Craig Taylor于2007年2月24日下午11:34发表以下内容:
Craig Taylor said the following on 2/24/2007 11:34 PM:

2月24日晚上11点01分,Jammer< ask ... @ mail。 comwrote:
On Feb 24, 11:01 pm, Jammer <ask...@mail.comwrote:

>我想做类似的事情:
var img_index =" dog";
document.images [img_index] = blah;

images [img_index]被视为图像[> img_index"]

我想在images数组中使用img_index的值。
>I want to do something like:
var img_index="dog";
document.images[img_index] = "blah";

images[img_index] is treated like images["img_index"]

I want to use the value of img_index in the images array.



使用FF我无法复制您的问题,但您可能想尝试

通过将索引括在()中来强制进行表达式评估'的;即:


document.images [(img_index)]


Using FF I was unable to duplicate your issue but you may want to try
forcing an expression evaluation by enclosing the index in ()''s; ie:

document.images[(img_index)]



您认为通过将其包装完成的确切内容( )?

部分问题是试图设置

document.images [var] =" blah"因为document.images [var]将引用一个对象,然后你试图将该对象设置为

到其他东西。设置图像的属性是一个不同的

场景。

-

兰迪

机会有利于准备好的心灵

comp.lang.javascript常见问题 - http:/ /jibbering.com/faq/index.html

Javascript最佳实践 - http://www.JavascriptToolbox.com/bestpractices/


Jammer写道:
Jammer wrote:

我想做类似的事情:

var img_index =" dog";

c


images [ img_index]被视为图像[" img_index"]


我想在images数组中使用img_index的值。
I want to do something like:
var img_index="dog";
c

images[img_index] is treated like images["img_index"]

I want to use the value of img_index in the images array.



我想:


document.images [img_index] =" blah";

其中img_index =" dog" ;;

如此有效。我想:

document.images [" dog"] =" blah";


但我想要:

img_index =" cat";

document.images [img_index] =" blah2";


这是我试过的代码:


函数openNav(button_name){

var img_path =" / images / nav /" + button_name +" .png";

document.images [(button_name)]。src = img_path;

}


我想打电话:

openNav(" dog");

openNav(" cat");

I want to:

document.images[img_index] = "blah";
where img_index = "dog";
so effectively. I want:
document.images["dog"] = "blah";

but then I want:
img_index="cat";
document.images[img_index] = "blah2";

Here is the code I tried:

function openNav( button_name ) {
var img_path = "/images/nav/" + button_name + ".png";
document.images[(button_name)].src = img_path;
}

I want to call:
openNav( "dog" );
openNav( "cat" );


这篇关于变量作为数组索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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