新手 关于js如何可以实现树状结构,外框必须带table tr td的html!!! 马上给分 把你的HTML代码结构贴出来看看... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 用dtree实现树状结构 {foreach item=nendo from=$data.list|smarty:nodefaults key=keynendo}{foreach item=kamoku from=$nendo|smarty:nodefaults key=keykamoku}{foreach item=ls from=$kamoku|smarty:nodefaults key=keyls}{if $ls.project_id eq ""} <tr class="listrecord" >{else} <tr class="listrecord" onmouseover="this.className='liston'" onmouseout="this.className='listrecord'">{/if}{if $keykamoku eq '1' && $keyls eq '1'} <td width="1%" align="left" nowrap>{$keynendo}年度</td>{else} <td width="1%" align="left" nowrap> </td>{/if}{if $keyls eq '1'} <td width="1%" align="left" nowrap>{$ls.str_kamoku}</td>{else} <td width="1%" align="left" nowrap> </td>{/if} <!--td width="1%" align="left" nowrap>{$ls.str_kubun}</td--> <td align="right" nowrap><span class="{$ls.goukei_color|default:"txt12"}">{$ls.goukei|custom_number_format}</span></td> <td align="right" nowrap><span class="{$ls.mon_1_color|default:"txt12"}">{$ls.mon_1|custom_number_format}</span></td> <td align="right" nowrap><span class="{$ls.mon_2_color|default:"txt12"}">{$ls.mon_2|custom_number_format}</span></td> <td align="right" nowrap><span class="{$ls.mon_3_color|default:"txt12"}">{$ls.mon_3|custom_number_format}</span></td> <td align="right" nowrap><span class="{$ls.mon_4_color|default:"txt12"}">{$ls.mon_4|custom_number_format}</span></td> <td align="right" nowrap><span class="{$ls.mon_5_color|default:"txt12"}">{$ls.mon_5|custom_number_format}</span></td> <td align="right" nowrap><span class="{$ls.mon_6_color|default:"txt12"}">{$ls.mon_6|custom_number_format}</span></td> <td align="right" nowrap><span class="{$ls.mon_7_color|default:"txt12"}">{$ls.mon_7|custom_number_format}</span></td> <td align="right" nowrap><span class="{$ls.mon_8_color|default:"txt12"}">{$ls.mon_8|custom_number_format}</span></td> <td align="right" nowrap><span class="{$ls.mon_9_color|default:"txt12"}">{$ls.mon_9|custom_number_format}</span></td> <td align="right" nowrap><span class="{$ls.mon_10_color|default:"txt12"}">{$ls.mon_10|custom_number_format}</span></td> <td align="right" nowrap><span class="{$ls.mon_11_color|default:"txt12"}">{$ls.mon_11|custom_number_format}</span></td> <td align="right" nowrap><span class="{$ls.mon_12_color|default:"txt12"}">{$ls.mon_12|custom_number_format}</span></td> </tr>{/foreach}{/foreach}{foreachelse} <tr class="listrecord"> <td colspan="16" align="center">{$smarty.const.PBMS_ERR_SEARCH_NODATA}</td> </tr>{/foreach}是PHP的 "<td width="1%" align="left" nowrap>{$keynendo}年度</td>" 这个就是第一个td我会在他前面再加一个td里面用来放+ - 图片 然后点击+,-号就会在每行夫数据下 显示子叶 是否有js可以实现 function FTree( oParent ) { this.myparent = oParent; var myself = this; var picUrl = "imgs/"; var treeStyle = new Array(); var picTop = 0; var isIE = false; this.doc; var textnode = '\ <table name="textnode" width="100%" border="0" cellspacing="0" cellpadding="0"><!-- 文本型节点 -->\n\ <tr height="18">\n\ <td>\n\ <div name="tag_bg"><!-- 背景 -->\n\ <img name="tag" /><!-- 前景 -->\n\ </div>\n\ </td>\n\ <td nowrap="nowrap" width="100%">\n\ <div name="doc_bg"><img name="doc" /></div><!-- 图标 -->\n\ <div name="text"> </div><!--文本-->\n\ </td>\n\ </tr>\n\ </table>\n\ '; var doctnode ='\n\ <table name="doctnode" width="100%" border="0" cellspacing="0" cellpadding="0" ><!-- 文本型节点 -->\n\ <tr height="18">\n\ <td>\n\ <div name="tag_bg"><!-- 背景 -->\n\ <img name="tag" /><!-- 前景 -->\n\ </div>\n\ </td>\n\ <td nowrap="nowrap" width="100%">\n\ <div name="doc_bg" ><img name="doc" /></div><!-- 图标 -->\n\ <div name="text"> </div><!--文本-->\n\ </td>\n\ </tr>\n\ <tr name="childbox" style="">\n\ <td name="null_bg"> </td>\n\ <td name="child" ></td>\n\ </tr>\n\ </table>\n\ '; //------------------------------------------------------------------------------------------- function getEleByName( tobj, stagName, sname ) { if(!tobj || tobj.nodeType!=1)return null; var otd = tobj.getElementsByTagName(stagName); for( var i=0 ;i<otd.length;i++ ) { if( otd[i].getAttribute("name") == sname ) return otd[i]; } return null; } //------------------------------------------------------------------------------------------- this.newRoot = function( toParent ) { var firstyle = treeStyle["tag_last"]; var osibling = toParent.childNodes; if( osibling.length == 0 ) firstyle = treeStyle["tag_first"]; else if( osibling.length >= 1 ) { var oPre = osibling[osibling.length-1]; if( osibling.length > 1 ) getEleByName( oPre, "div", "tag_bg" ).style.cssText = treeStyle["tag_midst"]; if( getEleByName( oPre, "TD", "null_bg" ) ) getEleByName( oPre, "TD", "null_bg" ).style.cssText = treeStyle["null_bg"]; } var odoc = document.createElement( "DIV" ); toParent.appendChild( odoc ); odoc.innerHTML = doctnode; getEleByName( odoc, "div", "tag_bg" ).style.cssText = firstyle; getEleByName( odoc, "img", "tag" ).src = picUrl+"close.gif"; getEleByName( odoc, "div", "doc_bg" ).style.cssText = treeStyle["null"]; getEleByName( odoc, "img", "doc" ).src = picUrl+"doc_close.gif"; getEleByName( odoc, "div", "text" ).style.cssText = treeStyle["text"]; getEleByName( odoc, "div", "text" ).innerHTML = "docs"; getEleByName( odoc, "TD", "null_bg").style.cssText = treeStyle["null"]; var orow = odoc.getElementsByTagName("TABLE")[0].rows[0]; orow.onclick = function() { var cbox = getEleByName( odoc, "TR", "childbox" ); if( cbox.style.display == "none" ) { getEleByName( odoc, "img", "tag" ).src = picUrl+"open.gif"; getEleByName( odoc, "img", "doc" ).src = picUrl+"doc_open.gif"; cbox.style.display = ""; } else { getEleByName( odoc, "img", "tag" ).src = picUrl+"close.gif"; getEleByName( odoc, "img", "doc" ).src = picUrl+"doc_close.gif"; cbox.style.display = "none"; } } orow.cells[1].onmouseover = function() { orow.cells[1].bgColor = "#EBEBEB";//#D2F0FF//#EBEBEB } orow.cells[1].onmouseout = function() { orow.cells[1].bgColor = ""; } var ochild = getEleByName( odoc, "td", "child" ); ochild.addroot = function(){ return myself.newRoot(this); }; ochild.addtext = function(){ myself.newNode(this); }; return ochild; } //------------------------------------------------------------------------------------------- this.newNode = function( toParent ) { var firstyle = treeStyle["tag_last"]; var osibling = toParent.childNodes; if( osibling.length == 0 ) firstyle = treeStyle["tag_first"]; else if( osibling.length >= 1 ) { var oPre = osibling[osibling.length-1]; if( osibling.length > 1 ) getEleByName( oPre, "div", "tag_bg" ).style.cssText = treeStyle["tag_midst"]; if( getEleByName( oPre, "TD", "null_bg" ) ) getEleByName( oPre, "TD", "null_bg" ).style.cssText = treeStyle["null_bg"]; } var odoc = document.createElement("DIV"); toParent.appendChild( odoc ); odoc.innerHTML = textnode; getEleByName( odoc, "div", "tag_bg" ).style.cssText = firstyle; getEleByName( odoc, "img", "tag" ).src = picUrl+"tag_last.gif"; getEleByName( odoc, "div", "doc_bg" ).style.cssText = treeStyle["null"]; getEleByName( odoc, "img", "doc" ).src = picUrl+"doc_file.gif"; getEleByName( odoc, "div", "text" ).style.cssText = treeStyle["text"]; var orow = odoc.getElementsByTagName("TABLE")[0].rows[0]; getEleByName( odoc, "div", "text" ).style.width = (orow.cells[1].offsetWidth -20) +"px"; getEleByName( odoc, "div", "text" ).innerHTML = "text"; orow.onclick = function() { //alert(""); } orow.cells[1].onmouseover = function() { orow.cells[1].bgColor = "#EBEBEB";//#D2F0FF//#EBEBEB } orow.cells[1].onmouseout = function() { orow.cells[1].bgColor = ""; } } //------------------------------------------------------------------------------------------- var tree_init = function() { var indx = 0; if( window.navigator.appName.indexOf("Microsoft") != -1 ) isIE = true; else picTop = 0; treeStyle["text"]="\n\ height: 18px;\n\ font-family: Arial, Helvetica, sans-serif;\n\ font-size: 12px;\n\ overflow-x: hidden;\n\ line-height: 18px;\n\ cursor: default;\n\ display: inline-block;\n\ float: left;\n\ "; treeStyle["null_bg"]="\n\ background-image:url("+picUrl+"tag_line_y.gif);\n\ "; treeStyle["null"]="\n\ width: 16px;\n\ height: 18px;\n\ clear: left;\n\ float: left;\n\ "; treeStyle["tag_line_x"]="\n\ width: 16px;\n\ height: 18px;\n\ background-image:url("+picUrl+"tag_line_x.gif);\n\ clear: left;\n\ float: left;\n\ "; treeStyle["tag_line_y"]="\n\ width: 16px;\n\ height: 18px;\n\ background-image:url("+picUrl+"tag_line_y.gif);\n\ background-repeat: repeat-y;\n\ float: left;\n\ position:static;\n\ "; treeStyle["tag_first"]="\n\ width: 16px;\n\ float: left;\n\ height: 18px;\n\ background-image:url("+picUrl+"tag_first.gif);\n\ "; treeStyle["tag_midst"]="\n\ width: 16px;\n\ float: left;\n\ height: 18px;\n\ background-image:url("+picUrl+"tag_midst.gif);\n\ "; treeStyle["tag_last"]="\n\ width: 16px;\n\ float: left;\n\ height: 18px;\n\ background-image:url("+picUrl+"tag_last.gif);\n\ "; this.doc = document.createDocumentFragment(); } tree_init(); } <script src="rtree.js" ></script></head><body><table width="200" border="1" cellpadding="0" cellspacing="0"> <tr> <td id="tree_box" > </td> </tr></table></body></html><script> var oTree = new FTree( document.getElementById("tree_box") ); var oroot = oTree.newRoot(oTree.myparent); oroot.addtext(); oroot.addtext(); oroot.addtext(); oroot.addtext(); oroot = oroot.addroot(); oroot.addtext(); oroot.addtext(); oTree.newNode(oTree.myparent); oTree.newRoot(oTree.myparent); oTree.newNode(oTree.myparent); oTree.newRoot(oTree.myparent); oTree.newNode(oTree.myparent);</script>正巧我也在写一个用表格堆出来的树,没写完,仅供参考,呵呵; 我的空间里有一个资源是用 MyEclpse 开发的,用层做的,加数据库,实现树状,带复选框,图片面性 + - 号的树状。你可以去下载.看适合你不??全源文件都在里面. 谁能告诉这是什么效果?给我个关键词,能给实现就更好了~~ 快,请帮我下,谢谢大哥。。。 我想用DOM的item取input中的值,如何取? 通过js读取cookies时中文出现乱码??? Html 动态添加解析的问题 求一个像Windows资源管理器这样的多级菜单 高手帮我看看这段代码 一个调用的问题 这段VBscript 脚本为什么不能运行?? 如何执行动态方法? JS日期选择器问题 能不能将下面的正则表达式合成一个?
{foreach item=kamoku from=$nendo|smarty:nodefaults key=keykamoku}
{foreach item=ls from=$kamoku|smarty:nodefaults key=keyls}
{if $ls.project_id eq ""}
<tr class="listrecord" >
{else}
<tr class="listrecord" onmouseover="this.className='liston'" onmouseout="this.className='listrecord'">
{/if}
{if $keykamoku eq '1' && $keyls eq '1'}
<td width="1%" align="left" nowrap>{$keynendo}年度</td>
{else}
<td width="1%" align="left" nowrap> </td>
{/if}
{if $keyls eq '1'}
<td width="1%" align="left" nowrap>{$ls.str_kamoku}</td>
{else}
<td width="1%" align="left" nowrap> </td>
{/if}
<!--td width="1%" align="left" nowrap>{$ls.str_kubun}</td-->
<td align="right" nowrap><span class="{$ls.goukei_color|default:"txt12"}">{$ls.goukei|custom_number_format}</span></td>
<td align="right" nowrap><span class="{$ls.mon_1_color|default:"txt12"}">{$ls.mon_1|custom_number_format}</span></td>
<td align="right" nowrap><span class="{$ls.mon_2_color|default:"txt12"}">{$ls.mon_2|custom_number_format}</span></td>
<td align="right" nowrap><span class="{$ls.mon_3_color|default:"txt12"}">{$ls.mon_3|custom_number_format}</span></td>
<td align="right" nowrap><span class="{$ls.mon_4_color|default:"txt12"}">{$ls.mon_4|custom_number_format}</span></td>
<td align="right" nowrap><span class="{$ls.mon_5_color|default:"txt12"}">{$ls.mon_5|custom_number_format}</span></td>
<td align="right" nowrap><span class="{$ls.mon_6_color|default:"txt12"}">{$ls.mon_6|custom_number_format}</span></td>
<td align="right" nowrap><span class="{$ls.mon_7_color|default:"txt12"}">{$ls.mon_7|custom_number_format}</span></td>
<td align="right" nowrap><span class="{$ls.mon_8_color|default:"txt12"}">{$ls.mon_8|custom_number_format}</span></td>
<td align="right" nowrap><span class="{$ls.mon_9_color|default:"txt12"}">{$ls.mon_9|custom_number_format}</span></td>
<td align="right" nowrap><span class="{$ls.mon_10_color|default:"txt12"}">{$ls.mon_10|custom_number_format}</span></td>
<td align="right" nowrap><span class="{$ls.mon_11_color|default:"txt12"}">{$ls.mon_11|custom_number_format}</span></td>
<td align="right" nowrap><span class="{$ls.mon_12_color|default:"txt12"}">{$ls.mon_12|custom_number_format}</span></td>
</tr>
{/foreach}
{/foreach}
{foreachelse}
<tr class="listrecord">
<td colspan="16" align="center">{$smarty.const.PBMS_ERR_SEARCH_NODATA}</td>
</tr>
{/foreach}是PHP的 "<td width="1%" align="left" nowrap>{$keynendo}年度</td>" 这个就是第一个td我会在他前面再加一个td里面用来放+ - 图片 然后点击+,-号就会在每行夫数据下 显示子叶 是否有js可以实现
function FTree( oParent )
{
this.myparent = oParent;
var myself = this;
var picUrl = "imgs/";
var treeStyle = new Array();
var picTop = 0;
var isIE = false;
this.doc;
var textnode = '\
<table name="textnode" width="100%" border="0" cellspacing="0" cellpadding="0"><!-- 文本型节点 -->\n\
<tr height="18">\n\
<td>\n\
<div name="tag_bg"><!-- 背景 -->\n\
<img name="tag" /><!-- 前景 -->\n\
</div>\n\
</td>\n\
<td nowrap="nowrap" width="100%">\n\
<div name="doc_bg"><img name="doc" /></div><!-- 图标 -->\n\
<div name="text"> </div><!--文本-->\n\
</td>\n\
</tr>\n\
</table>\n\
';
var doctnode ='\n\
<table name="doctnode" width="100%" border="0" cellspacing="0" cellpadding="0" ><!-- 文本型节点 -->\n\
<tr height="18">\n\
<td>\n\
<div name="tag_bg"><!-- 背景 -->\n\
<img name="tag" /><!-- 前景 -->\n\
</div>\n\
</td>\n\
<td nowrap="nowrap" width="100%">\n\
<div name="doc_bg" ><img name="doc" /></div><!-- 图标 -->\n\
<div name="text"> </div><!--文本-->\n\
</td>\n\
</tr>\n\
<tr name="childbox" style="">\n\
<td name="null_bg"> </td>\n\
<td name="child" ></td>\n\
</tr>\n\
</table>\n\
';
//-------------------------------------------------------------------------------------------
function getEleByName( tobj, stagName, sname )
{
if(!tobj || tobj.nodeType!=1)return null;
var otd = tobj.getElementsByTagName(stagName);
for( var i=0 ;i<otd.length;i++ )
{ if( otd[i].getAttribute("name") == sname )
return otd[i];
}
return null;
}
//-------------------------------------------------------------------------------------------
this.newRoot = function( toParent )
{
var firstyle = treeStyle["tag_last"];
var osibling = toParent.childNodes;
if( osibling.length == 0 )
firstyle = treeStyle["tag_first"];
else
if( osibling.length >= 1 )
{ var oPre = osibling[osibling.length-1];
if( osibling.length > 1 )
getEleByName( oPre, "div", "tag_bg" ).style.cssText = treeStyle["tag_midst"];
if( getEleByName( oPre, "TD", "null_bg" ) )
getEleByName( oPre, "TD", "null_bg" ).style.cssText = treeStyle["null_bg"];
}
var odoc = document.createElement( "DIV" );
toParent.appendChild( odoc );
odoc.innerHTML = doctnode;
getEleByName( odoc, "div", "tag_bg" ).style.cssText = firstyle;
getEleByName( odoc, "img", "tag" ).src = picUrl+"close.gif";
getEleByName( odoc, "div", "doc_bg" ).style.cssText = treeStyle["null"];
getEleByName( odoc, "img", "doc" ).src = picUrl+"doc_close.gif";
getEleByName( odoc, "div", "text" ).style.cssText = treeStyle["text"];
getEleByName( odoc, "div", "text" ).innerHTML = "docs";
getEleByName( odoc, "TD", "null_bg").style.cssText = treeStyle["null"];
var orow = odoc.getElementsByTagName("TABLE")[0].rows[0];
orow.onclick = function()
{
var cbox = getEleByName( odoc, "TR", "childbox" );
if( cbox.style.display == "none" )
{ getEleByName( odoc, "img", "tag" ).src = picUrl+"open.gif";
getEleByName( odoc, "img", "doc" ).src = picUrl+"doc_open.gif";
cbox.style.display = "";
}
else
{ getEleByName( odoc, "img", "tag" ).src = picUrl+"close.gif";
getEleByName( odoc, "img", "doc" ).src = picUrl+"doc_close.gif";
cbox.style.display = "none";
}
}
orow.cells[1].onmouseover = function()
{
orow.cells[1].bgColor = "#EBEBEB";//#D2F0FF//#EBEBEB
}
orow.cells[1].onmouseout = function()
{
orow.cells[1].bgColor = "";
}
var ochild = getEleByName( odoc, "td", "child" );
ochild.addroot = function(){ return myself.newRoot(this); };
ochild.addtext = function(){ myself.newNode(this); };
return ochild;
}
//-------------------------------------------------------------------------------------------
this.newNode = function( toParent )
{ var firstyle = treeStyle["tag_last"];
var osibling = toParent.childNodes;
if( osibling.length == 0 )
firstyle = treeStyle["tag_first"];
else
if( osibling.length >= 1 )
{ var oPre = osibling[osibling.length-1];
if( osibling.length > 1 )
getEleByName( oPre, "div", "tag_bg" ).style.cssText = treeStyle["tag_midst"];
if( getEleByName( oPre, "TD", "null_bg" ) )
getEleByName( oPre, "TD", "null_bg" ).style.cssText = treeStyle["null_bg"];
}
var odoc = document.createElement("DIV");
toParent.appendChild( odoc );
odoc.innerHTML = textnode;
getEleByName( odoc, "div", "tag_bg" ).style.cssText = firstyle;
getEleByName( odoc, "img", "tag" ).src = picUrl+"tag_last.gif";
getEleByName( odoc, "div", "doc_bg" ).style.cssText = treeStyle["null"];
getEleByName( odoc, "img", "doc" ).src = picUrl+"doc_file.gif";
getEleByName( odoc, "div", "text" ).style.cssText = treeStyle["text"];
var orow = odoc.getElementsByTagName("TABLE")[0].rows[0];
getEleByName( odoc, "div", "text" ).style.width = (orow.cells[1].offsetWidth -20) +"px";
getEleByName( odoc, "div", "text" ).innerHTML = "text";
orow.onclick = function()
{
//alert("");
}
orow.cells[1].onmouseover = function()
{
orow.cells[1].bgColor = "#EBEBEB";//#D2F0FF//#EBEBEB
}
orow.cells[1].onmouseout = function()
{
orow.cells[1].bgColor = "";
}
}
//-------------------------------------------------------------------------------------------
var tree_init = function()
{ var indx = 0;
if( window.navigator.appName.indexOf("Microsoft") != -1 )
isIE = true;
else
picTop = 0;
treeStyle["text"]="\n\
height: 18px;\n\
font-family: Arial, Helvetica, sans-serif;\n\
font-size: 12px;\n\
overflow-x: hidden;\n\
line-height: 18px;\n\
cursor: default;\n\
display: inline-block;\n\
float: left;\n\
";
treeStyle["null_bg"]="\n\
background-image:url("+picUrl+"tag_line_y.gif);\n\
";
treeStyle["null"]="\n\
width: 16px;\n\
height: 18px;\n\
clear: left;\n\
float: left;\n\
";
treeStyle["tag_line_x"]="\n\
width: 16px;\n\
height: 18px;\n\
background-image:url("+picUrl+"tag_line_x.gif);\n\
clear: left;\n\
float: left;\n\
";
treeStyle["tag_line_y"]="\n\
width: 16px;\n\
height: 18px;\n\
background-image:url("+picUrl+"tag_line_y.gif);\n\
background-repeat: repeat-y;\n\
float: left;\n\
position:static;\n\
";
treeStyle["tag_first"]="\n\
width: 16px;\n\
float: left;\n\
height: 18px;\n\
background-image:url("+picUrl+"tag_first.gif);\n\
";
treeStyle["tag_midst"]="\n\
width: 16px;\n\
float: left;\n\
height: 18px;\n\
background-image:url("+picUrl+"tag_midst.gif);\n\
";
treeStyle["tag_last"]="\n\
width: 16px;\n\
float: left;\n\
height: 18px;\n\
background-image:url("+picUrl+"tag_last.gif);\n\
";
this.doc = document.createDocumentFragment();
}
tree_init();
}
<script src="rtree.js" ></script>
</head>
<body>
<table width="200" border="1" cellpadding="0" cellspacing="0">
<tr>
<td id="tree_box" >
</td>
</tr>
</table>
</body>
</html>
<script>
var oTree = new FTree( document.getElementById("tree_box") );
var oroot = oTree.newRoot(oTree.myparent);
oroot.addtext();
oroot.addtext();
oroot.addtext();
oroot.addtext();
oroot = oroot.addroot();
oroot.addtext();
oroot.addtext();
oTree.newNode(oTree.myparent);
oTree.newRoot(oTree.myparent);
oTree.newNode(oTree.myparent);
oTree.newRoot(oTree.myparent);
oTree.newNode(oTree.myparent);
</script>正巧我也在写一个用表格堆出来的树,没写完,仅供参考,呵呵;