본문 바로가기
프로그래밍 개발/JS 웹

Javascript Text 객체

by Jinseok Kim 2020. 11. 20.
반응형

 

 

 

Text 객체

 

 

텍스트 객체는 텍스트 노드에 대한 DOM 객체로 CharcterData를 상속 받는다. 

 

 

 

 

 

 

<p id="jinseok1"><span>kim jin seok</span></p>

<p id="jinseok2">
    <span>kim jin seok</span>
</p> <!--줄바꿈이 존재-->

<script>
var t1 = document.getElementById('jinseok1').firstChild;
var t2 = document.getElementById('jinseok2').firstChild;
 
console.log(t1.firstChild.nodeValue);
try{
    console.log(t2.firstChild.nodeValue);   
} catch(e){
    console.log(e);
}
console.log(t2.nextSibling.firstChild.nodeValue);
 
</script>

 DOM에서는 공백이나 줄바꿈도 텍스트 노드다.

 

 

 

 

텍스트 노드의 값을 가져오는 API

  • data
  • nodeValue

 

조작

  • appendData()
  • deleteData()
  • insertData()
  • replaceData()
  • subStringData()

 

 

생성

  • docuemnt.createTextNode()

 

 

 

 

 

 

반응형

댓글