12345678910111213141516171819202122232425262728293031 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <ul id="list">
- <li>1</li>
- <li>2</li>
- <li>3</li>
- </ul>
- <button id="btn">添加</button>
- <script>
- var oBtn = document.getElementById("btn");
- var oList = document.getElementById("list");
- var num = 3;
- oBtn.onclick = function(){
- // 创建元素
- var oLi = document.createElement("li");
- num++;
- oLi.innerText = num;
- // 将元素插入到页面中
- oList.append(oLi);
- // oList.appendChild(oLi);
- }
- </script>
- </body>
- </html>
|