언어/Javascript

innerHTML vs innerText vs textContent

개발자국S2 2023. 6. 13. 17:49

html

<div id='content'>
 hello
 <span style='display:none'> this is innerText </span>
</div>

script / result

const content = document.getElementById("content")

console.log(content.innerHTML)
>> hello
>> <span>this is innertext</span>

console.log(content.innerText)
>> hello

console.log(content.textContent)
>> hello
>> this is innertext

 

 

출처 : 

 

[JavaScript] innerHTML, innerText, textContent 차이

innerHTML, innerText, textContent 속성은 텍스트를 읽어오고 설정할 수 있다는 점에서 비슷해보이지만, 조금씩 다른 차이가 있고 이 차이를 잘 알고 써야한다.먼저 innerHTML은 'Element'의 속성으로, element내

velog.io

 

반응형