HTML DOM innerText 속성은 HTML 요소의 내부 텍스트 콘텐츠를 반환하고 수정할 수 있도록 합니다.
구문
다음은 구문입니다 -
1. 내부 텍스트 반환
object.innerText
2. 내부 텍스트 설정
object.innerText=”text”
예시
innerText 속성의 예를 살펴보겠습니다 -
<!DOCTYPE html>
<html>
<head>
<style>
body{
text-align:center;
}
.box{
background-color:#347924;
width:100px;
padding:10px;
text-align:center;
font-weight:bold;
font-size:1.5rem;
color:white;
margin:1rem auto;
}
</style>
</head>
<body>
<h1>innerText property Example</h1>
<div class="box">
</div>
<button onclick="writeHi()">Write Hi</button>
<button onclick="writeHello()">Write Hello</button>
<script>
function writeHi() {
var outerBox = document.querySelector('.box');
outerBox.innerText ='HI';
}
function writeHello() {
var outerBox = document.querySelector('.box');
outerBox.innerText ='HELLO';
}
</script>
</body>
</html> 출력
이것은 다음과 같은 출력을 생성합니다 -

"Write Hi/Write Hello를 클릭합니다. ” 버튼을 눌러 녹색 상자의 내부 텍스트를 변경합니다.
