1. 程式人生 > >動態建立樹節點tree

動態建立樹節點tree

<!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>