<script language="javascript" type="text/javascript">
function window.onload() {
document.getElementById("TreeView1").onclick = OnTreeNodeChecked;//这里把TreeView名改一下~
}
function OnTreeNodeChecked() {
var ele = event.srcElement;
if(ele.type=='checkbox') {
ele.className = "";
var childrenDivID = ele.id.replace('CheckBox','Nodes');
var div = document.getElementById(childrenDivID);
if(div!=null) {
var checkBoxs = div.getElementsByTagName('INPUT');
for(var i=0;i<checkBoxs.length;i++) {
if(checkBoxs[i].type=='checkbox')
checkBoxs[i].checked=ele.checked;
}
}
setParentClassName(ele);
}
}
function setParentClassName(currCheckBox) {
var objParentNode= public_GetParentNode(currCheckBox);
if(public_IsObjectNull(objParentNode))
return;
var checkBoxs = objParentNode.getElementsByTagName("INPUT");
var allSelect = true;
var allUnSelect = true;
for(var i=0;i<checkBoxs.length;i++) {
if(checkBoxs[i].type=='checkbox')
{
if(checkBoxs[i].checked) allUnSelect = false;
else allSelect = false;
if((!allSelect) && (!allUnSelect))
break;
}
}
var objParentCheckBox = public_Node2CheckBox(objParentNode);
if(public_IsObjectNull(objParentCheckBox)) return;
setParentClassName(objParentCheckBox);
if(allSelect) {
objParentCheckBox.className = "";
objParentCheckBox.checked = true;
return;
}
if(allUnSelect) {
objParentCheckBox.className = "";
objParentCheckBox.checked = false;
return;
}
objParentCheckBox.checked = false;
objParentCheckBox.className = "parentbox_style";
}
//得到本节点所在的Node(Div对象)
function public_GetParentNode(element) {
var parent = element.parentNode;
var upperTagName = "DIV";
//如果这个元素还不是想要的tag就继续上溯
while (parent && (parent.tagName.toUpperCase() != upperTagName)) {
parent = parent.parentNode ? parent.parentNode : parent.parentElement;
}
return parent;
}
function public_Node2CheckBox(element) {
var objID = element.getAttribute("ID");
objID = objID.substring(0,objID.indexOf("Nodes"));
return document.getElementById(objID+"CheckBox");
}
function public_IsObjectNull(element) {
if(element==null || element == "undefined")
return true;
else
return false;
}
</script>