html怎样实现树形菜单,javascript实现树形菜单的方法

var ME={

ini:{i:true,d:{},d1:{},h:0,h1:0,h2:0},

html:function(da,f){

var s='

  • ';

for(var i=0,l=da.length;i

if(typeof(da[i].pid)=='object'){

s+='

'+da[i].ming+'';

this.ini.i=false;

s+=this.html(da[i].pid,(i+1==l));

}else{

s+='

'+da[i].ming+'';

}

s+='

';

}

s+='

';

return s;

},

st:function(id,da){

document.getElementById(id).innerHTML=this.html(da);

},

yc:function(a){

var s=a.className.substr(1);

if(s%2){

this.ini.d=a.parentNode.children[2];

this.ini.h1=this.hei(this.ini.d);

this.ini.h3=this.ini.h1/10;

this.yc1(1);

a.className='s'+(parseInt(s)+1);

a.parentNode.children[1].children[0].className='r2';

var ol=a.parentNode.parentNode._;

if(typeof(ol)=='object'){

this.ini.d1=ol;

this.ini.h=ol.offsetHeight;

this.ini.h2=this.ini.h/10;

this.yc2(1);

ol.parentNode.children[0].className='s'+(parseInt(ol.parentNode.children[0].className.substr(1))-1);

ol.parentNode.children[1].children[0].className='r1';

}

a.parentNode.parentNode._=a.parentNode.children[2];

}else{

this.ini.d1=a.parentNode.children[2];

this.ini.h=this.ini.d1.offsetHeight;

this.ini.h2=this.ini.h/10;

this.yc2(1);

a.className='s'+(parseInt(s)-1);

a.parentNode.children[1].children[0].className='r1';

a.parentNode.parentNode._='';

}

},

yc1:function(b){

var h1=ME.ini.h1-(11/(b+1)-1)*ME.ini.h3;

with(ME.ini.d.style){

height=h1+'px';

display='block';

}

if(b<10){

setTimeout('ME.yc1('+(b+1)+')',25);

}else{

ME.ini.d.style.display='block';

ME.ini.d.style.height='';

}

},

yc2:function(b){

var h1=(11/(b+1)-1)*ME.ini.h2;

with(ME.ini.d1.style){

height=h1+'px';

}

if(b<10){

setTimeout('ME.yc2('+(b+1)+')',25);

}else{

ME.ini.d1.style.display='';

ME.ini.d1.style.height='';

}

},

hei:function(a){

var b=a.cloneNode(true);

b.style.position='absolute';

b.style.display='block';

b.style.visibility='hidden';

a.parentNode.appendChild(b);

var h=b.offsetHeight;

a.parentNode.removeChild(b);

return h;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue2中实现树形菜单可以通过递归组件的方式来完成。下面是一个简单的示例: 首先,你需要有一个数据源,该数据源包含了树形结构的数据。例如: ```javascript data() { return { menuData: [ { id: 1, name: '菜单1', children: [ { id: 2, name: '菜单1-1', children: [] }, { id: 3, name: '菜单1-2', children: [ { id: 4, name: '菜单1-2-1', children: [] } ] } ] }, // 其他菜单项... ] } } ``` 然后,你可以创建一个递归组件,该组件用于渲染树形菜单的每个子项。例如: ```html <template> <ul> <li v-for="item in menuData" :key="item.id"> {{ item.name }} <TreeMenu :menuData="item.children" v-if="item.children.length > 0" /> </li> </ul> </template> <script> export default { name: 'TreeMenu', props: { menuData: { type: Array, required: true } } } </script> ``` 在上面的代码中,我们使用了`v-if`指令来判断当前菜单项是否有子菜单,如果有则递归地渲染`TreeMenu`组件,将子菜单作为其`menuData`属性传递进去。 最后,在你的父组件中,你可以使用这个递归组件来渲染整个树形菜单: ```html <template> <div> <TreeMenu :menuData="menuData" /> </div> </template> <script> import TreeMenu from './TreeMenu.vue' export default { name: 'App', components: { TreeMenu }, data() { return { menuData: [ // 菜单数据... ] } } } </script> ``` 这样,你就可以通过递归组件的方式实现树形菜单了。每个子菜单项都会根据是否有子菜单进行渲染,从而形成树形结构。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值