js 树形 菜单,为ul加name

JavaScript011

js 树形 菜单,为ul加name,第1张

$(".toclevel-1>ul").attr(name:"e_"+i)

=>

$(".toclevel-1>ul").attr("name","e_"+i)

这个地方写错了。

// 生成树结构

function tree(list) {

const result = []

for (let value of list) {

// 排除空字符串的情况

if (!value) {

continue

}

const values = value.split('/')

// 查找树结构的当前级别是否已经存在,不存在则创建对象,并添加入列表。

let current = result.find(item =>item.name === values[0])

if (current === void 0) {

current = {}

result.push(current)

}

for (let i = 0, length = values.lengthi <lengthi++) {

current.name = values[i]

if (i <length - 1) {

// 如果还有下一级内容,判断当前是否有 children,没有则构建.

if (current.children === void 0) {

current.children = []

}

// 查找下一级对象,为下一遍遍历构建对象

let nextCurrent = current.children.find(item =>item.name === values[i + 1])

if (nextCurrent === void 0) {

nextCurrent = {}

current.children.push(nextCurrent)

}

current = nextCurrent

}

}

}

return result

}

============ 假装分割线 ===========

以上代码是生成树的函数,调用 tree 函数并传入你的 input 数据,返回值就是生成的树。百科没找到传代码的地方了。

1、手动的改成树形结构,在二级opint标签的内容前面加入空格,形成树形的样子。

2、用div等标签做成模拟的select下拉框。

目前除此之外应该没有其它的办法能做到。

不要找我要代码,自己试试。