根据id获取父对象,然后使用childNodes获取所有子对象数组,关键代码:
document.getElementById(div_id).childNodes; // 子对象数组
实现代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <style type="text/css"> .red{color:red !important;} </style> <script type="text/javascript"> function fun(){ var objs = document.getElementById("test").childNodes; console.log(objs); for(k in objs) { if (objs[k].id==undefined) { continue; } alert(objs[k].id); } } </script> </head> <body> <div id="test"> <a id="link1" href="#">我是超链接</a> <input id="txt1" type="text" value="我是文本框"> <div id="div1">我是子div</div> </div> <input type='button' value='获取test里所有的子集id' onclick="fun()"/> </body> </html>
以上就是js getelementbyid后获取下一级的ID的方法,不知道大家有什么更优的方案吗,如果有的话,记得分享给我哦,感谢您访问“我爱捣鼓(www.woaidaogu.com)”网站的内容,希望对大家有所帮助!引用本文内容时,请注明出处!谢谢合作!