get循环中的getElementById,其for target div id在for循环中循环。 [英] getElementById in a for loop whose target div id is being looped in that for loop.

查看:101
本文介绍了get循环中的getElementById,其for target div id在for循环中循环。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!感谢先阅读我的问题!


我有很多带数字ID的div,例如< div

id =" 999">。 ..< / div>,< div id =" 1000"> ...< / div>


我希望做这样的事情,


for(var i = startvalue; i< = endvalue; i ++){

document.getElementById(i).style.background =" yello w;" ;;

}


但在第一次更改背景后,它会停止并告诉我

document.getElementById(i )没有属性。我相信那些

div'存在。


出了什么问题?


非常感谢!

解决方案

Nik在12/15/2006 12:08 AM说了以下内容:
< blockquote class =post_quotes>
您好!感谢您先阅读我的问题!



是什么让你觉得我读到它第一次而不是第二个?


我有许多带有数字ID的div,例如< div

id =" 999" ;> ...< / div>,< div id =" 1000"> ...< / div>


我希望做类似的事情这个,
for(var i = startvalue; i< = endvalue; i ++){

document.getElementById(i).style.background =" yello w;" ;;

}


但是在第一次更改背景后,它停止并告诉我

文件.getElementById(i)没有属性。而且我确信那些

div'存在。


出了什么问题?



代码中的某些东西不起作用?显示一个示例页面,其中包含最小的行为示例。要么你的结束值是错误的(取决于你设置它的价格是多少?)或者id不存在。


-

Randy

机会有利于准备好的心灵

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

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


嘿兰迪!谢谢你来救援。


不幸的是(真的!)所有div都是用我的

公司的密码从数据库中提取的。 ...但我已经尝试更改

document.getElementById(i).style.background =''yello w;'';警告(i);


这是有效的 - 我推动(endvalue - startvalue)时间OK我自己。

但为什么不是getElementById (i)工作,这是非常非常奇怪的对我来说,我感觉到一些真正愚蠢的东西,但是我无法弄清楚

我自己......

有什么想法吗?


Quintillion谢谢你

Randy Webb写道:


Nik在12/15/2006 12:08 AM说了以下内容:


您好!感谢您先阅读我的问题!



是什么让你觉得我读到它第一次而不是第二个?


我有许多带有数字ID的div,例如< div

id =" 999" ;> ...< / div>,< div id =" 1000"> ...< / div>


我希望做类似的事情这个,
for(var i = startvalue; i< = endvalue; i ++){

document.getElementById(i).style.background =" yello w;" ;;

}


但是在第一次更改背景后,它停止并告诉我

文件.getElementById(i)没有属性。而且我确信那些

div'存在。


出了什么问题?



代码中的某些东西不起作用?显示一个示例页面,其中包含最小的行为示例。要么你的结束值是错误的(取决于你设置它的价格是多少?)或者id不存在。


-

Randy

机会有利于准备好的心灵

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

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


Nik写道:
< blockquote class =post_quotes>
您好!感谢先阅读我的问题!


我有很多带数字ID的div,例如< div

id =" 999">。 ..< / div>,< div id =" 1000"> ...< / div>



对于有效的HTML,id属性的值不能以

数字开头,尽管它可以包含数字。


>

我希望做这样的事情,


for(var i = startvalue ; i< = endvalue; i ++){

document.getElementById(i).style.background =" yello w;" ;;

}


但在第一次更改背景后,它会停止并告诉我

document.getElementById(i)没有属性。而且我确信那些

div'存在。


出了什么问题?



谁知道?您发布的代码应该有效。要修复无效的ID,

只需在值前添加一个或多个字母,例如:


< script type =" text / javascript"> ;


函数foo(startvalue,endvalue){

for(var i = startvalue; i< = endvalue; i ++){

document.getElementById(''x''+ i).style.background =" yellow;" ;;

}

}


< / script>

< input type =" button" value =" call foo(1,2)" onclick =" foo(1,2);">

< div id =" x0"> x0< / div>

< div id =" x1"> x1< / div>

< div id =" x2"> x2< / div>

< div id = " x3"> x3< / div>

-

Rob


Hello! Thanks for reading my question first!

I have many divs with an numeral id ,for example <div
id="999">...</div>, <div id="1000">...</div>

and I wish to do something like this,

for( var i = startvalue; i<=endvalue; i++){
document.getElementById(i).style.background="yello w;";
}

but after the first changing background, it stops and tell me that the
document.getElementById(i) has no properties. And I am sure that those
div''s exist.

What''s going wrong?

Thanks So Much!

解决方案

Nik said the following on 12/15/2006 12:08 AM:

Hello! Thanks for reading my question first!

What makes you think I read it "first" and not "second"?

I have many divs with an numeral id ,for example <div
id="999">...</div>, <div id="1000">...</div>

and I wish to do something like this,

for( var i = startvalue; i<=endvalue; i++){
document.getElementById(i).style.background="yello w;";
}

but after the first changing background, it stops and tell me that the
document.getElementById(i) has no properties. And I am sure that those
div''s exist.

What''s going wrong?

Something in your code isn''t working? Show a sample page with a minimal
example of the behavior. Either your endvalue is wrong (depending on how
you set it) or the id doesn''t exist.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Hey Randy! Thanks for coming to rescue.

Unfortunately(really!) All the div''s are pulled from a database with my
company''s password.... BUT I have tried changing the
document.getElementById(i).style.background=''yello w;''; to alert(i);

And that works - I pushed (endvalue - startvalue) times of OK myself.
But why doesn''t the getElementById(i) work, this is very, very strange
to me, I am sensing something real stupid, but I cannot figure that out
myself...

any thoughts?

Quintillion Thank You''s
Randy Webb wrote:

Nik said the following on 12/15/2006 12:08 AM:

Hello! Thanks for reading my question first!


What makes you think I read it "first" and not "second"?

I have many divs with an numeral id ,for example <div
id="999">...</div>, <div id="1000">...</div>

and I wish to do something like this,

for( var i = startvalue; i<=endvalue; i++){
document.getElementById(i).style.background="yello w;";
}

but after the first changing background, it stops and tell me that the
document.getElementById(i) has no properties. And I am sure that those
div''s exist.

What''s going wrong?


Something in your code isn''t working? Show a sample page with a minimal
example of the behavior. Either your endvalue is wrong (depending on how
you set it) or the id doesn''t exist.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Nik wrote:

Hello! Thanks for reading my question first!

I have many divs with an numeral id ,for example <div
id="999">...</div>, <div id="1000">...</div>

For valid HTML, the value of the id attribute must not start with a
digit, though it can contain digits.

>
and I wish to do something like this,

for( var i = startvalue; i<=endvalue; i++){
document.getElementById(i).style.background="yello w;";
}

but after the first changing background, it stops and tell me that the
document.getElementById(i) has no properties. And I am sure that those
div''s exist.

What''s going wrong?

Who knows? Your code as posted should work. To fix the invalid ids,
just prefix the values with one or more letters, e.g.:

<script type="text/javascript">

function foo(startvalue, endvalue){
for( var i=startvalue; i<=endvalue; i++){
document.getElementById(''x'' + i).style.background="yellow;";
}
}

</script>
<input type="button" value="call foo(1,2)" onclick="foo(1,2);">
<div id="x0">x0</div>
<div id="x1">x1</div>
<div id="x2">x2</div>
<div id="x3">x3</div>
--
Rob


这篇关于get循环中的getElementById,其for target div id在for循环中循环。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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