以JSON数据源用HTML实现三级树

<!DOCTYPE html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>树形菜单示例</title>
    <style type="text/css">
     ul>li{
         list-style: none;
     }
       /* 可展开*/
      .switch-open
      {
          margin-left:-12px;
          border:6px solid transparent;
          display:inline-block;
          width:0px;
          height:0px;
          border-top-color: black;
 
      }
       /* 展开完毕*/
     .switch-close
     {
         margin-left:-12px;
         border:6px solid transparent;
         display:inline-block;
         width:0px;
         height:0px;
         border-left-color: black;
         margin-bottom: 2px;
 
     }
       /* 改变CheckBox样式*/
     /* input[type='checkbox']{
         width: 20px;
         height: 20px;
         border: 1px solid #c9c9c9;
         border-radius: 3px;
         outline: none;
         color:white;
         text-align: center;
     }
     input[type='checkbox']:before
     {
         content: '√ ';
         color:transparent;
     }
     input[type=checkbox]:checked{
         background-color: #30add6;
     }
     input[type=checkbox]:checked:before{
         content: '√';
         color:white;
         font-weight: bold;
     } */
 
 
    </style>
</head>
<body>
<div class="warp">
    <ul id="container">
    </ul>
</div>
 
 
<script type="text/javascript">
 
    //结构
    var json={
        '0-0':{
            '0-0-0':null,
            '0-0-1':{
                '0-0-1-0':null,
                '0-0-1-1':null,
                '0-0-1-2':null
            },
            '0-0-2':null
        },
        '0-1':{
            '0-1-0':null,
            '0-1-1':null
        },
        '0-2':null
    };
 
//这里生成DOM
    function generate(json,par)
    {
        for(var attr in json)
        {
            var ele=document.createElement('li');
            if(!json[attr])
                ele.innerHTML=''+attr;
            else
            {
                ele.innerHTML='<span><span class="switch-open" onclick="toggle(this)"></span>'+attr+'</span>';
                var nextpar=document.createElement('ul');
                ele.appendChild(nextpar);
                generate(json[attr],nextpar);
            }
            par.appendChild(ele);
        }
    }
    generate(json,document.getElementById('container'));
 
    //处理展开和收起
function toggle(eve)
{
    var par=eve.parentNode.nextElementSibling;
    if(par.style.display=='none')
    {
        par.style.display='block';
        eve.className='switch-open';
 
    }
    else
    {
        par.style.display='none';
        eve.className='switch-close';
    }
}
 
// //处理全部勾选和全部不选
//     function checkChange(eve)
//     {
//         var oul=eve.parentNode.nextElementSibling;
//         if(eve.checked)
//         {
//                 for(var i=0;i<oul.querySelectorAll('input').length;i++)
//                 {
//                     oul.querySelectorAll('input')[i].checked=true;
//                 }
//         }
//         else
//         {
//             for(var i=0;i<oul.querySelectorAll('input').length;i++)
//             {
//                 oul.querySelectorAll('input')[i].checked=false;
//             }
//         }
//     }
</script>
 
 
 
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值