动态制作列表

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>动态创建列表</h1>
    <div id="box">

    </div>

    <script>
        var box = document.getElementById("box");

        var names = ["刘备","曹操","孙权","关羽"];

        //创建ul
        var ul = document.createElement("ul");
        box.appendChild(ul);

        //向ul中添加数据
        for(var i=0;i<names.length;i++){
            var li = document.createElement("li");
            ul.appendChild(li);

            //给每个li添加内容
            li.innerText = names[i];
        }
    </script>
</body>
</html>
posted @ 2021-08-13 18:14  charonmomo  阅读(41)  评论(0编辑  收藏  举报