最近在使用asp.net2.0中的TreeView控件时,遇到一个小困惑。
此控件的节点在没有赋予链接的情况下,点击是要回发的,当然,我更希望文本节点和它前面的图标一样,点击时只有展开和收缩的动作即可,而且不希望回发到服务端,其实如果换个javascript写成的树控件最好,可是偏偏咱又是个大懒人,所以情急之下干脆写了一个方法改变TreeView控件文本节点点击的行为,特记下以备后用。
1//去除树控件节点触发回发事件,点击节点和点击展开收缩图标相同的效果
2//如果有复选框,则点击节点与点击复选框相同效果
3function appendExpend(nam) {
4 //debugger
5 var tree = document.getElementById(nam);
6 var nodes = tree.getElementsByTagName("a");
7 var menuname = nam.toLowerCase() + "n"; //"TMenun";
8
9 for (i = 0; nodes[i]; i++) {
10 if (!nodes[i].id) continue;
11 id = nodes[i].id.toLowerCase();
12 if (id.indexOf(menuname) >= 0) {
13 var otherid = id.substr(menuname.length);
14 var othernode = document.getElementById(nam + "t" + otherid);
15 if (!othernode) continue;
16 othernode.href = "";
17 othernode.onclick = null;
18 othernode.href = nodes[i].href;
19 }
20 }
21
22
23 var inputs = tree.getElementsByTagName("input");
24 var chkname = "checkbox";
25 for (i = 0; inputs[i]; i++) {
26 if (!inputs[i].id) continue;
27 id = inputs[i].id.toLowerCase();
28 chkIndex = id.lastIndexOf(chkname);
29 namIndex = id.indexOf(menuname);
30 if (inputs[i].type == "checkbox" && chkIndex >= 0 && namIndex >= 0) {
31 otherid = id.substring(menuname.length, chkIndex);
32 othernode = document.getElementById(nam + "t" + otherid);
33 if (!othernode) continue;
34 othernode.href = "";
35 othernode.onclick = null;
36 othernode.href = "javascript:ShiftCheck('" + inputs[i].id + "');";
37 }
38 }
39}
2//如果有复选框,则点击节点与点击复选框相同效果
3function appendExpend(nam) {
4 //debugger
5 var tree = document.getElementById(nam);
6 var nodes = tree.getElementsByTagName("a");
7 var menuname = nam.toLowerCase() + "n"; //"TMenun";
8
9 for (i = 0; nodes[i]; i++) {
10 if (!nodes[i].id) continue;
11 id = nodes[i].id.toLowerCase();
12 if (id.indexOf(menuname) >= 0) {
13 var otherid = id.substr(menuname.length);
14 var othernode = document.getElementById(nam + "t" + otherid);
15 if (!othernode) continue;
16 othernode.href = "";
17 othernode.onclick = null;
18 othernode.href = nodes[i].href;
19 }
20 }
21
22
23 var inputs = tree.getElementsByTagName("input");
24 var chkname = "checkbox";
25 for (i = 0; inputs[i]; i++) {
26 if (!inputs[i].id) continue;
27 id = inputs[i].id.toLowerCase();
28 chkIndex = id.lastIndexOf(chkname);
29 namIndex = id.indexOf(menuname);
30 if (inputs[i].type == "checkbox" && chkIndex >= 0 && namIndex >= 0) {
31 otherid = id.substring(menuname.length, chkIndex);
32 othernode = document.getElementById(nam + "t" + otherid);
33 if (!othernode) continue;
34 othernode.href = "";
35 othernode.onclick = null;
36 othernode.href = "javascript:ShiftCheck('" + inputs[i].id + "');";
37 }
38 }
39}