欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

动态创建树节点tree

程序员文章站 2022-06-08 09:12:51
...
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<a id="create" href="javascript:void(0)">创建列表</a>

</body>
</html>

<script type="text/javascript">
    var create = document.getElementById("create");
    var ul = document.createElement("ol");
    var num = 0;
    create.addEventListener("click",function () {
        tree(ul,{"width":100})
    },false);


function tree(obj,paramas) {
    if ( paramas == undefined) {
        return;
    }else {
        var li = document.createElement("li");
        if (paramas.background !== undefined) {
            li.style.background = paramas.background.toString();
        }else {
            li.style.background = "blue";
        }
        if (paramas.width !== undefined) {
            li.style.width = parseFloat(paramas.width)+'px';
        }else {
            li.style.width = 50+'px';
        }
        obj.append(li);
        document.body.append(obj);
    }
}

</script>