//参数:容器控件、要查找的控件的id关键字、要查找的控件的标签名称
//返回值:查找到的控件id列表字符串,以逗号分割。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
function
GetIdListBySubKey(container,subKey,TagName)
{
var idList = "" ;
for ( var i = 0 ; i < container.childNodes.length;i ++ )
{
if (container.childNodes[i].nodeName == TagName && container.childNodes[i].id.indexOf(subKey) > - 1 )
{
idList += container.childNodes[i].id + " , " ;
}
if (container.childNodes[i].childNodes.length > 0 )
{
idList += GetIdListBySubKey(container.childNodes[i],subKey,TagName)
}
}
return idList;
}
{
var idList = "" ;
for ( var i = 0 ; i < container.childNodes.length;i ++ )
{
if (container.childNodes[i].nodeName == TagName && container.childNodes[i].id.indexOf(subKey) > - 1 )
{
idList += container.childNodes[i].id + " , " ;
}
if (container.childNodes[i].childNodes.length > 0 )
{
idList += GetIdListBySubKey(container.childNodes[i],subKey,TagName)
}
}
return idList;
}
可以用来获取GridView中的控件。
改进:可以去掉TagName参数
//获取某容器控件中id包含某字符串的控件id列表
//参数:容器控件、要查找的控件的id关键字
//返回值:查找到的控件id列表字符串,以逗号分割。
代码
function
GetIdListBySubKey(container,subIdKey)
{
var idList = "" ;
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 idList = "" ;
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;
}
如:GetIdListBySubKey(document,"txt_Money")
改进:直接返回控件数组
//获取某容器控件中id包含某字符串的控件数组
//参数:容器控件、要查找的控件的id关键字
//返回值:查找到的控件数组
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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;
}
{
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;
}