<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
body{
font-size:14px;
margin:0;}
div{
width:auto;
height:auto;
line-height:150%;}
ul{
list-style:none;
margin-left:-20px;}
ul li:hover{
background-color:#DDDDDD;
color:#FF0000;
cursor:pointer;}
</style>
</head>
<body>
<div id="continer"></div>
</body>
</html>
<script language="javascript" src="jquery.min.js"></script>
<script language="javascript">
//模拟数据
var jsonData = [
{"id":"1","name":"父节点1","url":"www.baidu.com","pid":"0"},
{"id":"4","name":"父节点11","url":"","pid":"1"},
{"id":"13","name":"父节点111","url":"","pid":"4"},
{"id":"14","name":"父节点112","url":"","pid":"4"},
{"id":"5","name":"父节点12","url":"","pid":"1"},
{"id":"6","name":"父节点13","url":"","pid":"1"},
{"id":"2","name":"父节点4","url":"","pid":"0"},
{"id":"7","name":"父节点41","url":"","pid":"2"},
{"id":"8","name":"父节点42","url":"","pid":"2"},
{"id":"9","name":"父节点43","url":"","pid":"2"},
{"id":"3","name":"父节点5","url":"","pid":"0"},
{"id":"10","name":"父节点51","url":"","pid":"3"},
{"id":"11","name":"父节点52","url":"","pid":"3"},
{"id":"12","name":"父节点53","url":"","pid":"3"}
] ;
//主方法,运用递归实现
function createTree(jsons,pid){
if(jsons != null){
var ul = '<ul class="">' ;
for(var i=0;i<jsons.length;i++){
if(jsons[i].pid == pid){
ul += '<li>' + jsons[i].name + "</li>" ;
ul += createTree(jsons,jsons[i].id) ;
}
}
ul += "</ul>" ;
}
return ul ;
}
$(function(){
var ul = createTree(jsonData,0) ; $("#continer").append(ul) ; //控制菜单的隐藏显示
$("ul[class] li").each(function(){
$(this).click(function(){
$(this).next().toggle() ;
}) ;
}) ; }) ;
</script>
</body>
</html>

js动态生成JSON树的更多相关文章

  1. JS 动态生成JSON对象

    JS 动态生成JSON对象,一般用到JSON传递参数的时候,会用到. function onGeneratedRow(columnsResult) { var jsonData = {}; colum ...

  2. 爬虫案例(js动态生成数据)

    需求:爬取https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.html页面中的 ...

  3. 抓取js动态生成的数据分析案例

    需求:爬取https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.html页面中的 ...

  4. js动态生成数据的抓取

    需求:爬取https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.html页面中的 ...

  5. JS动态生成的元素,其对应的方法不响应(比如单击事件,鼠标移动事件等)

    主要原因:在页面给元素注册点击事件的时候[ $(function () {  XXX }); ],JS动态生成的元素还尚未生成,所以click事件就没有生效 解决方法: 方案一:js动态生成元素后再给 ...

  6. 抓取Js动态生成数据且以滚动页面方式分页的网页

    代码也可以从我的开源项目HtmlExtractor中获取. 当我们在进行数据抓取的时候,如果目标网站是以Js的方式动态生成数据且以滚动页面的方式进行分页,那么我们该如何抓取呢? 如类似今日头条这样的网 ...

  7. js动态生成数据列表

    我们通常会使用table标签来展示数据内容,由于需要展示的数据内容是随时更换的,所以不可能将展示的数据列表写死在html写死在页面中,而是需要我们根据后台传来的数据随时更换,这个时候就需要我们使用js ...

  8. 2)JS动态生成HTML元素的爬取

    2)JS动态生成HTML元素的爬取 import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.W ...

  9. 泛型方法动态生成表达式树 Expression

    public string GetGridJSON(TraderInfo model) { IQueryable<TraderInfo> Temp = db.TraderInfo; if ...

随机推荐

  1. smartgit document Rebase

    The Rebase command allows you to apply commits from one branch to another. Rebase can be viewed as m ...

  2. PLSQL_性能优化系列07_Oracle Parse Bind Variables解析绑定变量

    2014-09-25 Created By BaoXinjian

  3. CF 369C . Valera and Elections tree dfs 好题

    C. Valera and Elections   The city Valera lives in is going to hold elections to the city Parliament ...

  4. centos nginx环境下删除CI框架Index.php入口遇到404问题

    今天在网上百度看了很多文章,想要去掉index.php入口文件有好多方法,自己也照着在网站到根目录下新建了一个.htaccess文件,内容如下: RewriteEngine On RewriteCon ...

  5. 错误代码2104:无法下载Silverlight应用程序。请查看Web服务器设置

    今天调试Silverlight程序,把ClientBin文件夹下的.xap文件删除后遇到这样一个问题:错误代码2104:无法下载Silverlight应用程序.请查看Web服务器设置.在网上查了一下, ...

  6. Spring中IoC的入门实例

    Spring中IoC的入门实例 Spring的模块化是很强的,各个功能模块都是独立的,我们可以选择的使用.这一章先从Spring的IoC开始.所谓IoC就是一个用XML来定义生成对象的模式,我们看看如 ...

  7. CentOS 安装 Chrome

    cd  /etc/yum.repos.d/ vi  google.repo [gogle] name=Google-x86_64 baseurl=http://dl.google.com/linux/ ...

  8. PS 的参考线

    新建参考线 菜单:"视图-新建参考线", 直接输入想要位置的参考线.   参考线对齐 按住Shift键拉辅助线,辅助线会自动对齐到当前标尺的刻度上.           按住Alt ...

  9. [datatable]C# DataTable 如何排序

    DataTable dt = new DataTable(); dt.Columns.Add("ProductID", typeof(string)); dt.Columns.Ad ...

  10. (easy)LeetCode 198.House Robber

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...