파란색은 property key, 보라색은 function임
[전체 접근]
[클래스에서 클래스 값 "일부"에 접근]
textContent : (공백 포함) text만 출력
innerHTML : (공백 포함) html 태그까지 출력
공백은 trim() 사용하여 제거 가능
<div id="greet">
Hello <span class="highlight">World!</span>
</div>
위와 같을 때,
=> textContent 출력값 : Hello World!
innerHTML 출력값 : Hello <span class="highlight">World!</span>
| 방법 | 장점 | 예시 코드 |
|---|---|---|
| addEventListener | - 여러 핸들러 추가 가능 - 구형 브라우저에서 호환성 문제 가능 |
element.addEventListener('click', handlerFunction); handlerFunction은 함수 이름만 사용하며, () 없이 쓴다. |
| HTML 태그에 직접 추가 | - JavaScript와 HTML이 혼합되어 유지보수 어려움 | <button>Click</button> |
| 이벤트 프로퍼티 (element.onclick) | - 하나의 핸들러만 지정 가능 (덮어쓰기) | element.onclick = function() { alert('Clicked!'); }; |
function hasChild($tag) {
// ************* !! : 0이면 false, 나머지는 true 반환해라
return !!$tag.children.length;
}
(1) $ul.replaceChildren()
(2) li => $li.remove());
(3) $ul.innerHTML = '';
=> html 태그가 아래와 같을 때,
<ul id="fruits">
<li>사과</li>
<li>바나나</li>
<li>포도</li>
</ul>
// id="fruits" 접근 + li 태그 접근
const $fruits = document.getElementById('fruits');
const [$apple, $banana, $grape] = $fruits;
// input 태그 생성
const $input = document.createElement('input');
$input.value = $apple.textContent; // input은 textContext가 아닌 value임
$fruits.replaceChild($input, $apple); // $input 태그 사용 완료
$grape.replaceWith($input);
<= !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
생성한 $input 태그는 한 번만 쓸 수 있으므로, 이 코드를 쓰는 동시에 위에서 $apple 대신 대체했던 $input는 사라지게 됨
input 태그에 초기에 value 속성이 없었을 경우, 사용자가 입력한 값은 $input.style.value.value 프로퍼티에는 저장되지 않고, $input.value 프로퍼티에만 저장됨 경우
attributes 말고 밖에 나와 있는 width, fontSize는 초기값으로 고정됨
image).width);을 통해 값 출력 가능