javascript循环增加子节点

javascript循环增加子节点,第1张

$(function() {

var html = ""

// 添加 5 行

for(var r = 0r <5r++) {

html += "<tr>"

// 每行添加 3 个单元格

for(var c = 0c <3c++) {

html += "<td>r" + r + "c" + c + "</td>"

}

html += "</tr>"

}

$("#table").append(html)

}) <table border="1" id="table"></table>

因为现在浏览器在解析你写的table部分代码时,会自加添加一个<tbody></tbody>的节点把你那的那三行包起来,这样一来,你写的每一行就不再是table直接的子节点了,而table的直接子节点成<tbody></tbody>了,所以,你用table来删除其行的子节点是不存在的。就是行每一行成了<table>的子节点<tbody>的子节点。

浏览器解析完成后是:

<table border = "2" width = "280" height = "100" id = "table">

<tbody >

<tr><th>姓名</th><th>email</th><th>电话</th><th></th></tr>

<tr id="a"><td>Tom</td><td>tom@tom.com</td><td>5000</td><td><a href = "javascript:void(0)">Delete</a></td>

</tr>

<tr><td>Jerry</td><td>jerry@sohu.com</td><td>8000</td><td><a href = "javascript:void(0)">Delete</a></td>

</tr>

</tbody>

</table>

这样的,所以,你还是自己主动加上tbody节点,然后把id=table放在tbody上,这样才能按你写的代码删除

从你的数据表中读出父子节点表到DataTable中,然后按上下级关系一个个添加就可以。添加父子节点只需另写一个功能很数据表中加就行,比较简单。

给你一个示例,大概是这样子。

private void ShowTree()

{

this.SystemTree.Nodes.Clear()

TreeNode rootNode = new TreeNode()

rootNode.Text = "XXXXXXXX"

rootNode.Value = ""

int status = 0

rootNode.NavigateUrl = "javascript:SelectNode('root',' '," + Convert.ToString(nodeid++) + "," + status + ")"

this.SystemTree.Nodes.Add(rootNode)

SubsystemManagement subSystem = new SubsystemManagement()

DataTable dt = new DataTable()

dt = subSystem.GetAllsubsystemInfo() //显示子系统名称

foreach (DataRow dr in dt.Rows)

{

TreeNode subNode = new TreeNode()

string subSysName = dr["CFunName"].ToString()

subNode.Text = subSysName

string subSysId = dr["CFunctionID"].ToString()

subNode.Value = subSysId

status = 1

subNode.NavigateUrl = "javascript:SelectNode('subsys','" + subSysId + "'," + Convert.ToString((nodeid++)) + "," + status + ")"

rootNode.ChildNodes.Add(subNode)

rootNode.Expanded = true

DataTable sdt = new DataTable()

sdt = subSystem.GetModulesBySubSysId(subSysId)//显示模块名称

foreach (DataRow sdr in sdt.Rows)

{

TreeNode sNode = new TreeNode()

string moduleName = sdr["CFunName"].ToString()

sNode.Text = moduleName

string moduleId = sdr["CFunctionId"].ToString()

sNode.Value = moduleId

status = 1

sNode.NavigateUrl = "javascript:SelectNode('module','" + moduleId + "'," + Convert.ToString((nodeid++)) + "," + status + ")"

subNode.ChildNodes.Add(sNode)

subNode.Expanded = true

DataTable fdt = new DataTable()

fdt = subSystem.GetFunctionsByModuleId(moduleId)//显示功能名称

foreach (DataRow fdr in fdt.Rows)

{

TreeNode fNode = new TreeNode()

fNode.Text = fdr["CFunName"].ToString()

fNode.Value = fdr["CFunctionId"].ToString()

status = 2

fNode.NavigateUrl = "javascript:SelectNode('function','" + fdr["CFunctionId"].ToString() + "'," + Convert.ToString((nodeid++)) + "," + status + ")"

sNode.ChildNodes.Add(fNode)

sNode.Expanded = false

}

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11816523.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存