div 내부에 배열의 요소를 포함하려면 배열을 반복하고 요소를 div에 계속 추가하기만 하면 됩니다.
이것은 다음과 같이 할 수 있습니다 -
예시
const myArray = ["stone","paper","scissors"];
const embedElements = () => {
myArray.forEach(element => {
document.getElementById('result').innerHTML +=
`<div>${element}</div><br />`;
// here result is the id of the div present in the DOM
});
}; 이 코드는 array의 요소를 표시하려는 div에 id가 'result'라고 가정합니다.
이에 대한 완전한 코드는 -
예시
<!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>
<div id="result"></div>
<button onclick="embedElements()">Show Data</button>
<script>
{
const myArray = ["stone","paper","scissors"];
function embedElements(){
myArray.forEach(el => {
document.getElementById('result').innerHTML +=`<div>${el}</div><br />`;
// here result is the id of the div present in the dom
});
};
}
</script>
</body>
</html> 출력

"데이터 표시" 버튼을 클릭하면 다음이 표시됩니다. -
