JS로 html에 image넣기

Heewon👩🏻‍💻·2024년 4월 26일
const images = [
  "back2.jpeg",
  "back3.jpeg",
  "back4.jpeg",
  "back5.jpeg",
  "back6.jpeg",
];

const chosenImage = images[Math.floor(Math.random() * images.length)];
const bgImage = document.createElement("img");
bgImage.src = `/img/${chosenImage}`;
document.body.appendChild(bgImage);

document.createElement("img")로 이미지 테그를 작성할 수 있다.

appendChild()를 통해 html body 마지막줄에 이미지를 넣을 수 있다.

prepend()는 html body 첫줄에 코드를 넣을 수 있음. 신기해 ㅋ

profile
Interested in coding, meat lover, in love with dogs , enjoying everything happens in my life.

0개의 댓글