本代码修改自网上的代码,主要实现了以下功能:
1、当一个节点选中时,其各级父节点均被选中,其子节点全部被选中
2、当一个节点取消选中时,其各级父节点如果没有其它子节点被选中,则相应取消选中状态;其子节点选中状态全部取消
3、获得选中的节点值
代码如下,未经优化,仅实现功能,欢迎大家指正:
Code
//获取元素指定tagName的父元素
function public_GetParentByTagName(element, tagName){
var parent = element.parentNode;
var upperTagName = tagName.toUpperCase();
//如果这个元素还不是想要的tag就继续上溯
while (parent && (parent.tagName.toUpperCase() != upperTagName))
{
parent = parent.parentNode ? parent.parentNode : parent.parentElement;
}
return parent;
}
//设置节点的父节点Cheched——该节点可访问,则他的父节点也必能访问
function setParentChecked(objNode) {
var objParentDiv = public_GetParentByTagName(objNode, "div");
if(objParentDiv==null || objParentDiv == "undefined")
{
return;
}
var objID = objParentDiv.getAttribute( "ID");
objID = objID.substring(0,objID.indexOf( "Nodes"));
objID = objID+ "CheckBox";
var objParentCheckBox = document.getElementById(objID);
if(objParentCheckBox==null || objParentCheckBox == "undefined")
{
return;
}
if(objParentCheckBox.tagName!= "INPUT" && objParentCheckBox.type == "checkbox")
return;
objParentCheckBox.checked = true;
setParentChecked(objParentCheckBox);
}
//设置节点的父节点uncheched——检查父节点的所有子节点是否都没有选中,如果没有则取消父节点的选中状态
function setParentUnChecked(objNode){
var objParentDiv = public_GetParentByTagName(objNode, "div");
if(objParentDiv==null || objParentDiv == "undefined")
{
return;
}
var objID = objParentDiv.getAttribute( "ID");
objID = objID.substring(0,objID.indexOf( "Nodes"));
objID = objID+ "CheckBox";
var objParentCheckBox = document.getElementById(objID);
if(objParentCheckBox==null || objParentCheckBox == "undefined")
{
return;
}
if(objParentCheckBox.tagName!= "INPUT" && objParentCheckBox.type == "checkbox")
return;
//parent.mainFrame.document.getElementById("textarea").value =objParentDiv.innerHTML;
var objchild = objParentDiv.getElementsByTagName('INPUT'); ;
var count = objchild.length;
var ischeck = false;
for(var i=0;i <objchild.length;i++)
{
var tempObj = objchild[i];
if(tempObj.tagName== "INPUT" && tempObj.type == "checkbox" && tempObj.checked)
{
ischeck = true;
return;
}
}
objParentCheckBox.checked = ischeck;
setParentUnChecked(objParentCheckBox);
}
//设置节点的子节点uncheched——该节点不可访问,则他的子节点也不能访问
function setChildUnChecked(divID)
{
var objchild = divID.children;
var count = objchild.length;
for(var i=0;i <objchild.length;i++)
{
var tempObj = objchild[i];
if(tempObj.tagName== "INPUT" && tempObj.type == "checkbox")
{
tempObj.checked = false;
}
setChildUnChecked(tempObj);
}
}
//设置节点的子节点cheched——该节点可以访问,则他的子节点也都能访问
function setChildChecked(divID)
{
//alert(divID.innerHTML);
parent.mainFrame.document.getElementById("textarea").value =divID.innerHTML;
var objchild = divID.children;
var count = objchild.length;
for(var i=0;i <objchild.length;i++)
{
var tempObj = objchild[i];
if(tempObj.tagName== "INPUT" && tempObj.type == "checkbox")
{
tempObj.checked = true;
}
setChildChecked(tempObj);
}
}
//触发事件
function CheckEvent(){
var objNode = event.srcElement;
if(objNode.tagName!= "INPUT" || objNode.type!= "checkbox")
return;
//选中
if(objNode.checked==true){
setParentChecked(objNode);
var objID = objNode.getAttribute("ID");
var objID = objID.substring(0,objID.indexOf( "CheckBox"));
var objParentDiv = document.getElementById(objID+ "Nodes");
if(objParentDiv==null || objParentDiv == "undefined"){
return;
}
setChildChecked(objParentDiv);
}else{ //取消选中
var objID = objNode.getAttribute("ID");
var objID = objID.substring(0,objID.indexOf("CheckBox"));
var objParentDiv = document.getElementById(objID+ "Nodes");
if(objParentDiv!=null && objParentDiv != "undefined") {
setChildUnChecked(objParentDiv);
}
setParentUnChecked(objNode);
}
}
//显示选中的节点
function ShowCheck(){
var inputs = document.getElementsByTagName("input");
var result = "";
//parent.mainFrame.document.getElementById("textarlea").value = document.body.innerHTML;
for(var i=inputs.length-1;i>=0;i--)
{
if(inputs[i].type=="checkbox"&& inputs[i].checked==true)
{
var title = inputs[i].nextSibling.title;
if(result.indexOf(title)==-1){
result += title+",";
alert(result);
}
}
}
result = result.substring(0,result.length-1);
return result;
}
//获取元素指定tagName的父元素
function public_GetParentByTagName(element, tagName){
var parent = element.parentNode;
var upperTagName = tagName.toUpperCase();
//如果这个元素还不是想要的tag就继续上溯
while (parent && (parent.tagName.toUpperCase() != upperTagName))
{
parent = parent.parentNode ? parent.parentNode : parent.parentElement;
}
return parent;
}
//设置节点的父节点Cheched——该节点可访问,则他的父节点也必能访问
function setParentChecked(objNode) {
var objParentDiv = public_GetParentByTagName(objNode, "div");
if(objParentDiv==null || objParentDiv == "undefined")
{
return;
}
var objID = objParentDiv.getAttribute( "ID");
objID = objID.substring(0,objID.indexOf( "Nodes"));
objID = objID+ "CheckBox";
var objParentCheckBox = document.getElementById(objID);
if(objParentCheckBox==null || objParentCheckBox == "undefined")
{
return;
}
if(objParentCheckBox.tagName!= "INPUT" && objParentCheckBox.type == "checkbox")
return;
objParentCheckBox.checked = true;
setParentChecked(objParentCheckBox);
}
//设置节点的父节点uncheched——检查父节点的所有子节点是否都没有选中,如果没有则取消父节点的选中状态
function setParentUnChecked(objNode){
var objParentDiv = public_GetParentByTagName(objNode, "div");
if(objParentDiv==null || objParentDiv == "undefined")
{
return;
}
var objID = objParentDiv.getAttribute( "ID");
objID = objID.substring(0,objID.indexOf( "Nodes"));
objID = objID+ "CheckBox";
var objParentCheckBox = document.getElementById(objID);
if(objParentCheckBox==null || objParentCheckBox == "undefined")
{
return;
}
if(objParentCheckBox.tagName!= "INPUT" && objParentCheckBox.type == "checkbox")
return;
//parent.mainFrame.document.getElementById("textarea").value =objParentDiv.innerHTML;
var objchild = objParentDiv.getElementsByTagName('INPUT'); ;
var count = objchild.length;
var ischeck = false;
for(var i=0;i <objchild.length;i++)
{
var tempObj = objchild[i];
if(tempObj.tagName== "INPUT" && tempObj.type == "checkbox" && tempObj.checked)
{
ischeck = true;
return;
}
}
objParentCheckBox.checked = ischeck;
setParentUnChecked(objParentCheckBox);
}
//设置节点的子节点uncheched——该节点不可访问,则他的子节点也不能访问
function setChildUnChecked(divID)
{
var objchild = divID.children;
var count = objchild.length;
for(var i=0;i <objchild.length;i++)
{
var tempObj = objchild[i];
if(tempObj.tagName== "INPUT" && tempObj.type == "checkbox")
{
tempObj.checked = false;
}
setChildUnChecked(tempObj);
}
}
//设置节点的子节点cheched——该节点可以访问,则他的子节点也都能访问
function setChildChecked(divID)
{
//alert(divID.innerHTML);
parent.mainFrame.document.getElementById("textarea").value =divID.innerHTML;
var objchild = divID.children;
var count = objchild.length;
for(var i=0;i <objchild.length;i++)
{
var tempObj = objchild[i];
if(tempObj.tagName== "INPUT" && tempObj.type == "checkbox")
{
tempObj.checked = true;
}
setChildChecked(tempObj);
}
}
//触发事件
function CheckEvent(){
var objNode = event.srcElement;
if(objNode.tagName!= "INPUT" || objNode.type!= "checkbox")
return;
//选中
if(objNode.checked==true){
setParentChecked(objNode);
var objID = objNode.getAttribute("ID");
var objID = objID.substring(0,objID.indexOf( "CheckBox"));
var objParentDiv = document.getElementById(objID+ "Nodes");
if(objParentDiv==null || objParentDiv == "undefined"){
return;
}
setChildChecked(objParentDiv);
}else{ //取消选中
var objID = objNode.getAttribute("ID");
var objID = objID.substring(0,objID.indexOf("CheckBox"));
var objParentDiv = document.getElementById(objID+ "Nodes");
if(objParentDiv!=null && objParentDiv != "undefined") {
setChildUnChecked(objParentDiv);
}
setParentUnChecked(objNode);
}
}
//显示选中的节点
function ShowCheck(){
var inputs = document.getElementsByTagName("input");
var result = "";
//parent.mainFrame.document.getElementById("textarlea").value = document.body.innerHTML;
for(var i=inputs.length-1;i>=0;i--)
{
if(inputs[i].type=="checkbox"&& inputs[i].checked==true)
{
var title = inputs[i].nextSibling.title;
if(result.indexOf(title)==-1){
result += title+",";
alert(result);
}
}
}
result = result.substring(0,result.length-1);
return result;
}
在服务端page_load中添加:
Code
protected void Page_Load(object sender, EventArgs e)
{
treeFileView.Attributes.Add("OnClick", "CheckEvent()");
}
protected void Page_Load(object sender, EventArgs e)
{
treeFileView.Attributes.Add("OnClick", "CheckEvent()");
}