本文介绍了get循环中的getElementById,其for target div id在for循环中循环。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 您好!感谢先阅读我的问题! 我有很多带数字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'存在。 出了什么问题? 非常感谢! 解决方案 是什么让你觉得我读到它第一次而不是第二个? 代码中的某些东西不起作用?显示一个示例页面,其中包含最小的行为示例。要么你的结束值是错误的(取决于你设置它的价格是多少?)或者id不存在。 - Randy 机会有利于准备好的心灵 comp.lang.javascript常见问题 - http://jibbering.com/faq Javascript最佳实践 - http://www.JavascriptToolbox.com/bestpractices/ 是什么让你觉得我读到它第一次而不是第二个? 代码中的某些东西不起作用?显示一个示例页面,其中包含最小的行为示例。要么你的结束值是错误的(取决于你设置它的价格是多少?)或者id不存在。 - Randy 机会有利于准备好的心灵 comp.lang.javascript常见问题 - http://jibbering.com/faq Javascript最佳实践 - http://www.JavascriptToolbox.com/bestpractices/ 对于有效的HTML,id属性的值不能以 数字开头,尽管它可以包含数字。 谁知道?您发布的代码应该有效。要修复无效的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 <divid="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 thedocument.getElementById(i) has no properties. And I am sure that thosediv''s exist.What''s going wrong?Thanks So Much! 解决方案What makes you think I read it "first" and not "second"?Something in your code isn''t working? Show a sample page with a minimalexample of the behavior. Either your endvalue is wrong (depending on howyou set it) or the id doesn''t exist.--RandyChance Favors The Prepared Mindcomp.lang.javascript FAQ - http://jibbering.com/faqJavascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/What makes you think I read it "first" and not "second"?Something in your code isn''t working? Show a sample page with a minimalexample of the behavior. Either your endvalue is wrong (depending on howyou set it) or the id doesn''t exist.--RandyChance Favors The Prepared Mindcomp.lang.javascript FAQ - http://jibbering.com/faqJavascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/For valid HTML, the value of the id attribute must not start with adigit, though it can contain digits.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循环中循环。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-26 06:56