<html>
태그는 <head>
및 <body>
로 분기되는 루트이며, 이는 <title>
, <div>
및 <p>
과 같은 다른 요소로 추가로 분기됩니다. JavaScript를 사용하면 이러한 요소에 액세스하고 수정하고 추가하고 제거하고 재정렬할 수 있습니다.
const element = document.getElementById('myId');
const elements = document.getElementsByClassName('myClass');
const paragraphs = document.getElementsByTagName('p');
const firstElement = document.querySelector('.myClass'); // First element matching the selector const allElements = document.querySelectorAll('.myClass'); // All matching elements
document.getElementById('example').textContent = 'New Text';
document.getElementById('example').innerHTML = '<b>Bold Text</b>';
const image = document.querySelector('img'); image.setAttribute('src', 'newImage.jpg'); image.setAttribute('alt', 'New Description');
.style
또는 CSS 클래스를 사용하여 요소 모양 변경:
const box = document.getElementById('box'); box.style.backgroundColor = 'blue'; box.style.color = 'white'; box.classList.add('active'); box.classList.remove('inactive'); box.classList.toggle('highlight');
const newDiv = document.createElement('div'); newDiv.textContent = 'I am a new div!'; document.body.appendChild(newDiv);
const element = document.getElementById('removeMe'); element.remove();
click
, mouseover
, keydown
등
.parentNode
, .parentElement
, .children
, .childNodes
, .nextElementSibling
, .previousElementSibling
등의 속성을 사용하여 DOM 트리를 탐색하세요.
documentFragment
을 사용하세요.innerHTML
: 더 나은 성능과 보안을 위해 createElement
를 사용하세요.위 내용은 JavaScript의 DOM 조작 이해: 초보자 가이드의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!