파싱
웹 브라우저가, 코드를 컴파일링 하면서
<script>
를 만나면, HTML 의 파싱을 중단하고,<script>
부분부터 파싱하기 시작한다.
여러개의 node 와 string 을 추가할수 있다.
마지막 자식 뒤에 node 또는 DOMString 개체 집합을 삽입한다.
return 값을 반환하지 않는다.
const div = document.createElement('div')
const span = document.createElement('span')
const p = document.createElement('p')
div.append(span)
span.append('span의 텍스트컨텐츠')
/*-------------------------------------------------
<div>
<span>span의 텍스트컨텐츠</span>
</div>
--------------------------------------------------*/
const div = document.createElement('div')
const span = document.createElement('span')
const p = document.createElement('p')
document.body.append(div, 'hello', span, p)
console.log(document.body.append(div, 'hello', span, p)) // undefined
/*-------------------------------------------------
<body>
<div></div>
hello
<span></span>
<p></p>
</body>
--------------------------------------------------*/
const div = document.createElement('div')
const span = document.createElement('span')
const p = document.createElement('p')
/*-------------------------------------------------
<div>
<span></span>
</div>
--------------------------------------------------*/
const span = document.createElement('span')
span.appendChild('span의 텍스트컨텐츠') // TypeError
const div = document.createElement('div')
const span = document.createElement('span')
console.log(document.body.appendChild(div))
/*-------------------------------------------------
<div></div>
--------------------------------------------------*/
★ textContent 와의 차이점은?
.innerHTML 은 해당 요소의 자손들의 모든 HTML 내용 모두를 반환하지만,
.textContent 는 딱, 단일 text 노드만을 반환한다.<!--.innerHTML 사용 시--> <p> </p> <!-------------------------------------------> <script> let str = '화면에 결과가 표시됩니다' function add(result){ const p = document.querySelector('p') p.innerHTML = `<h1 style="color:blue">` + result + `</h1>` } add(str) </script> <!-- < 화면 결과 > 화면에 결과가 표시됩니다. -->
<!--.textContent 사용시--> <p> </p> <!-------------------------------------------> <script> let str = '화면에 결과가 표시됩니다' function add(result){ const p = document.querySelector('p') p.textContent = `<h1 style="color:blue">` + result + `</h1>` } add(str) </script> <!-- < 화면 결과 > <h1 style="color:blue">결과</h1> -->
<input id="user" type="text" value ="joo">
<script>
const $input = document.getElementById('user')
console.log($input.hasAttribute('value')) // <- HTML 어트리뷰트 존재확인
console.log($input.getAttribute('value')) // <- HTML 어트리뷰트 참조
console.log($input.setAttribute('value', 'foo')) // <- HTML 어트리뷰터 변경
console.log($input.removeAttribute('value')) // <- HTML 어트리뷰터 삭제
// <input id="user" type="text" value ="foo">
</script>