JS 获取某个容器控件中id包含制定字符串的控件id列表
//获取某容器控件中id包含某字符串的控件id列表 //参数:容器控件、要查找的控件的id关键字 function GetIdListBySubKey(container,subIdKey) { var idList = ""; var child=container.childNodes; var chileLength=child.length; for(var i = 0; i < container.childNodes.length;i++) { if(container.childNodes[i].attributes != null && container.childNodes[i].attributes["id"] != undefined && container.childNodes[i].id.indexOf(subIdKey) > -1) { idList += container.childNodes[i].id + ","; } if(container.childNodes[i].childNodes.length > 0) { idList += GetIdListBySubKey(container.childNodes[i],subIdKey) } } return idList; } //调用 var doms=GetIdListBySubKey(document,"Money"); //document console.log(doms);
//改进:直接返回控件数组
代码如下:
//改进:直接返回控件数组 //获取某容器控件中id包含某字符串的控件数组 //参数:容器控件、要查找的控件的id关键字 //返回值:查找到的控件数组 function GetConListBySubKey(container,subIdKey) { var reConArry = []; for(var i = 0; i < container.childNodes.length;i++) { if(container.childNodes[i].attributes != null && container.childNodes[i].attributes["id"] != undefined && container.childNodes[i].id.indexOf(subIdKey) > -1) { reConArry.push(container.childNodes[i]); } if(container.childNodes[i].childNodes.length > 0) { var re = GetConListBySubKey(container.childNodes[i],subIdKey) for(var k = 0;k<re.length;k++) { reConArry.push(re[k]); } } } return reConArry; }
斩后知