透過querySelector()可以取得HTML Elememt Object
<!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>
<button>創建項目</button>
</body>
<script src="./app.js"></script>
</html>
let button = document.querySelector("button"); //取得button
let body = document.querySelector("body"); //取得body
let count = 1;
button.addEventListener("click", () => { //按鈕點擊事件
let myH = document.createElement("h1"); //創建h1
myH.innerText = "項目" + count;
body.appendChild(myH); //添加至body裡
count++;
});
當點擊按鈕時會跳出項目
【以上為我的學習心得,如有錯誤歡迎糾正】