Skip to content

Commit c0ca2ae

Browse files
committed
add: 랜덤 색상으로 배경색 바꾸는 버튼 이벤트 추가
1 parent 5f1f16f commit c0ca2ae

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

project1-colors/src/Button.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,11 @@ export default function Button({ $target, onClick }) {
22
const $button = document.createElement("button");
33
$target.append($button);
44
$button.textContent = "Click Me!";
5+
6+
$button.addEventListener("click", (e) => {
7+
const generateRandomColorCode = () => {
8+
return Math.floor(Math.random() * 16777215).toString(16);
9+
};
10+
onClick(generateRandomColorCode());
11+
});
512
}

project1-colors/src/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ const $app = document.querySelector("#app");
44

55
new Button({
66
$target: $app,
7+
onClick: (colorCode) => {
8+
$app.style.backgroundColor = `#${colorCode}`;
9+
},
710
});

0 commit comments

Comments
 (0)