放一段选取treeview控件生成树形菜单复选框所有子节点的js代码
<script language="javascript" type="text/javascript">
var Obj_;
document.onclick=function()
{
if(event.srcElement.tagName=="INPUT")
{
Obj_=event.srcElement;
getNextElement(event.srcElement);
}
}
function getNextElement(srcObj)
{
var i=0;
while(srcObj.tagName!="TABLE")
{
srcObj=srcObj.parentNode;
i++;
if(i>20) break;
}
//
if(srcObj.nextSibling==null)
{
selectParentNode(srcObj);
return;
}
if(srcObj.nextSibling.tagName=="DIV")
{
var targetObj=srcObj.nextSibling;
selectThem(targetObj);
}
// else if(srcObj.nextSibling.tagName=="TABLE")
// {
// //
// }
selectParentNode(srcObj);
}
function selectThem(targetObj_)
{
var ChexBox_Obj=targetObj_.getElementsByTagName("INPUT");
for(var i=0;i<ChexBox_Obj.length;i++)
{
ChexBox_Obj[i].checked=Obj_.checked;
}
}
function selectParentNode(obj)
{
while(obj.parentNode.previousSibling.tagName=="TABLE")
{
//
var parentObj=obj.parentNode;
var ChexBox_brother=parentObj.getElementsByTagName("INPUT");
var counter_=0;
for(var i=0;i<ChexBox_brother.length;i++)
{
if(ChexBox_brother[i].checked) counter_++;
}
//
var parentNContiner=obj.parentNode.previousSibling;
var CheckB=parentNContiner.getElementsByTagName("INPUT");
CheckB[0].checked=counter_>0?true:false;
obj=parentNContiner;
}
}
</script>
var Obj_;
document.onclick=function()
{
if(event.srcElement.tagName=="INPUT")
{
Obj_=event.srcElement;
getNextElement(event.srcElement);
}
}
function getNextElement(srcObj)
{
var i=0;
while(srcObj.tagName!="TABLE")
{
srcObj=srcObj.parentNode;
i++;
if(i>20) break;
}
//
if(srcObj.nextSibling==null)
{
selectParentNode(srcObj);
return;
}
if(srcObj.nextSibling.tagName=="DIV")
{
var targetObj=srcObj.nextSibling;
selectThem(targetObj);
}
// else if(srcObj.nextSibling.tagName=="TABLE")
// {
// //
// }
selectParentNode(srcObj);
}
function selectThem(targetObj_)
{
var ChexBox_Obj=targetObj_.getElementsByTagName("INPUT");
for(var i=0;i<ChexBox_Obj.length;i++)
{
ChexBox_Obj[i].checked=Obj_.checked;
}
}
function selectParentNode(obj)
{
while(obj.parentNode.previousSibling.tagName=="TABLE")
{
//
var parentObj=obj.parentNode;
var ChexBox_brother=parentObj.getElementsByTagName("INPUT");
var counter_=0;
for(var i=0;i<ChexBox_brother.length;i++)
{
if(ChexBox_brother[i].checked) counter_++;
}
//
var parentNContiner=obj.parentNode.previousSibling;
var CheckB=parentNContiner.getElementsByTagName("INPUT");
CheckB[0].checked=counter_>0?true:false;
obj=parentNContiner;
}
}
</script>
想必大家都是触类旁通的,这段代码适合vs2005中treeview控件生成的树形菜单,以下是代码片段:
<asp:TreeView ID="TreeView_1" ShowCheckBoxes="all" runat="server" ShowLines="true" >