front-end/JavaScript
[Vanilla JS - Chrome App] #3.1 <Background> Js 에서 Html element 생성하기
gaan
2022. 7. 17. 00:46
1. Javascript에서 html element을 생성
how to use
// images 배열 생성
const images = ["0.jpg", "1.jpg", "2.jpg", "3.jpg", "4.jpg"];
// images 랜덤으로 선택
const chosenImage = images[Math.floor(Math.random() * images.length)];
// html에 img element 생성
const bgImage = document.createElement("img");
// <image src="img/0.jpg"> (랜덤 생성)
bgImage.src = `img/${chosenImage}`;
background.js
2. body에 html을 추가
how to use
document.body.appendChild(bgImage);
background.js