HTML DOM innerHTML 속성을 반환하고 요소의 내부 HTML을 수정할 수 있습니다. 내부 HTML은 콘텐츠입니다.
구문
다음은 구문입니다 -
1. innerHTML 반환
object.innerHTML
2. 내부 HTML 설정
object.innerHTML=”value”
여기에서 "가치 "는 콘텐츠 또는 중첩된 HTML 요소를 나타냅니다.
예시
innerHTML 속성의 예를 보자 -
<!DOCTYPE html>
<html>
<head>
<style>
body{
text-align:center;
}
.outer-box{
background-color:#347924;
width:200px;
height:200px;
padding:10px;
text-align:center;
font-weight:bold;
color:white;
margin:1rem auto;
}
.inner-box{
width:100px;
height:100px;
margin:3rem auto;
background-color:#8dce79;
}
.inner-circle{
width:100px;
height:100px;
border-radius:50%;
background-color:#8dce79;
margin:3rem auto;
}
</style>
</head>
<body>
<h1>innerHTML property Example</h1>
<div class="outer-box">
</div>
<button onclick="getsquare()">Get Square</button>
<button onclick="getcircle()">Get Circle</button>
<script>
function getsquare() {
var outerBox = document.querySelector('.outer-box');
outerBox.innerHTML ='<div class="inner-box">square</div>';
}
function getcircle() {
var outerBox = document.querySelector('.outer-box');
outerBox.innerHTML ='<div class="inner-circle">circle</div>';
}
</script>
</body>
</html> 출력
이것은 다음과 같은 출력을 생성합니다 -

"스퀘어 가져오기를 클릭합니다. ” 버튼을 누르면 녹색 사각형 안에 사각형이 표시됩니다.

이제 "서클 가져오기를 클릭하세요. ” 버튼을 누르면 녹색 사각형 안에 원이 표시됩니다.
