我使用以下 js 代码展开/折叠 ASP.Net TreeView 控件的所有节点:
// handle tree - this doesn't seem to work when the tree contains node images
function TreeviewExpandCollapseAll(treeViewId, expandAll) {
var displayState = (expandAll == true ? "none" : "block");
var treeView = document.getElementById(treeViewId);
if (treeView) {
var treeLinks = treeView.getElementsByTagName("a");
var nodeCount = treeLinks.length;
alert(nodeCount);
for (i = 0; i < nodeCount; i++) {
if (treeLinks[i].firstChild.tagName) {
if (treeLinks[i].firstChild.tagName.toLowerCase() == "img") {
var currentToggleLink = treeLinks[i];
var childContainer = GetParentByTagName("table", currentToggleLink).nextSibling;
if (childContainer.style.display == displayState) {
eval(currentToggleLink.href);
}
}
}
} //for loop ends
}
}
//utility function to get the container of an element by tagname
function GetParentByTagName(parentTagName, childElementObj) {
var parent = childElementObj.parentNode;
while (parent.tagName.toLowerCase() != parentTagName.toLowerCase()) {
parent = parent.parentNode;
}
return parent;
}
问题是当我有NodeStyle-ImageUrl="img/FolderSmall.gif"时,打开第一个子节点后扩展失败 - 仅当存在节点图像时。
JS 错误是 childContainer 为空:
if (childContainer.style.display == displayState) {
eval(currentToggleLink.href);
我确定其他人也遇到过这种情况,但是我为展开/折叠找到的所有代码都在同一点失败。