首页 > 代码库 > TreeView控件点击父节点同时选择子节点JS脚本

TreeView控件点击父节点同时选择子节点JS脚本

 1  <script language="javascript" type="text/javascript"> 2         function client_OnTreeNodeChecked() { 3             var obj = window.event.srcElement; 4             var treeNodeFound = false; 5             var checkedState; 6             if (obj.tagName == "INPUT" && obj.type == "checkbox") { 7                 var treeNode = obj; 8                 checkedState = treeNode.checked; 9                 do {10                     obj = obj.parentElement;11                 }12                 while (obj.tagName != "TABLE")13                 var parentTreeLevel = obj.rows[0].cells.length;14                 var parentTreeNode = obj.rows[0].cells[0];15                 var tables = obj.parentElement.getElementsByTagName("TABLE");16                 var numTables = tables.length17                 if (numTables >= 1) {18                     for (i = 0; i < numTables; i++) {19                         if (tables[i] == obj) {20                             treeNodeFound = true;21                             i++;22                             if (i == numTables) {23                                 return;24                             }25                         }26                         if (treeNodeFound == true) {27                             var childTreeLevel = tables[i].rows[0].cells.length;28                             if (childTreeLevel > parentTreeLevel) {29                                 var cell = tables[i].rows[0].cells[childTreeLevel - 1];30                                 var inputs = cell.getElementsByTagName("INPUT");31                                 if (inputs.length > 0) {32                                     inputs[0].checked = checkedState;33                                 }34                             }35                             else { return; }36                         }37                     }38                 }39             }40         }41     </script>
    控件上调用:
        <asp:TreeView ID="TreeView1" runat="server" onclick="client_OnTreeNodeChecked();">
        </asp:TreeView>

 

TreeView控件点击父节点同时选择子节点JS脚本