찾다
  • 로그인
  • 가입
비밀번호 재설정 성공

당신이 관심을 갖고 있는 프로젝트를 팔로우하고 그들에 관한 최신 뉴스를 맛보세요

HTML DOM 탐색

수집 49
읽다 20172
업데이트 시간 2016-09-11

HTML DOM을 사용하면 노드 관계를 사용하여 노드 트리에서 탐색할 수 있습니다.


HTML DOM 노드 목록

getElementsByTagName() 메서드는 노드 목록을 반환합니다. 노드 목록은 노드의 배열입니다.

다음 코드는 문서의 모든 <p> 노드를 선택합니다.

Instance

<html><!DOCTYPE html>
<html>
<body>

<p>Hello World!</p>
<p>The DOM is very useful!</p>

<script>
x=document.getElementsByTagName("p");
document.write("The innerHTML of the second paragraph is: " + x[1].innerHTML);
</script>

</body>
</html>

Run Instance»

온라인 인스턴스를 보려면 "인스턴스 실행" 버튼을 클릭하세요

참고:

다음 라벨은 0부터 시작합니다.


HTML DOM 노드 목록 길이

길이 속성은 노드 목록의 노드 수를 정의합니다.

길이 속성을 사용하여 노드 목록을 반복할 수 있습니다.

Instances

<html><!DOCTYPE html>
<html>
<body>

<p>Hello World!</p>
<p>The DOM is very useful!</p>
<p>This example demonstrates the <b>length</b> property.</p>

<script>
x=document.getElementsByTagName("p");
for (i=0;i<x.length;i++)
  { 
  document.write(x[i].innerHTML);
  document.write("<br>");
  }
</script>
</body>
</html>

Run Instance»

온라인 인스턴스를 보려면 "Run Instance" 버튼을 클릭하세요

인스턴스 분석:

  • 모든<p> 요소 노드 가져오기

  • 각 <p> 요소의 텍스트 노드 값 출력


Navigation 노드 관계

parentNode, firstChild 및 lastChild, 문서 구조 탐색에서.

아래 HTML 스니펫을 참조하세요.

<html>
<body>

<p>Hello World!</p>
<div>
<p>DOM은 매우 유용합니다!</p>
<p>이 예는 노드 관계를 보여줍니다.</p>
</div>

</body>
</html>
  • 첫 번째 <p> 요소는 <body> ; 요소의 첫 번째 하위 요소(firstChild)

  • <div> 요소는 <body> 요소의 마지막 하위 요소(lastChild)입니다.

  • <body> <div> 요소의 상위 노드(parentNode)

firstChild 속성을 사용하여 요소의 텍스트에 액세스할 수 있습니다:

Instance

<html><!DOCTYPE html>
<html>
<body>

<p id="intro">Hello World!</p>

<script>
x=document.getElementById("intro");
document.write(x.firstChild.nodeValue);
</script>

</body>
</html>

인스턴스 실행»

보려면 "인스턴스 실행" 버튼을 클릭하세요. 온라인 인스턴스


DOM 루트 노드

여기에는 모든 문서에 액세스할 수 있는 두 가지 특수 속성이 있습니다:

  • document.documentElement - 모든 문서

  • document.body - 문서 본문

인스턴스

<html><!DOCTYPE html>
<html>
<body>

<p>Hello World!</p>
<div>
<p>The DOM is very useful!</p>
<p>This example demonstrates the <b>document.body</b> property.</p>
</div>

<script>
alert(document.body.innerHTML);
</script>

</body>
</html>

인스턴스 실행»

온라인 인스턴스를 보려면 "인스턴스 실행" 버튼을 클릭하세요


childNodes 및 nodeValue

innerHTML 속성 외에도 childNodes 및 nodeValue 속성을 사용하여 요소의 콘텐츠를 가져올 수도 있습니다.

다음 코드는 id="intro"를 사용하여 <p> 요소의 값을 가져옵니다.

Instance

<html><!DOCTYPE html>
<html>
<body>

<p id="intro">Hello World!</p>

<script>
txt=document.getElementById("intro").childNodes[0].nodeValue;
document.write(txt);
</script>

</body>
</html>

Run Instance»

온라인 인스턴스를 보려면 "인스턴스 실행" 버튼을 클릭하세요.

위의 예에서 getElementById는 메소드이고 childNodes 및 nodeValue는 속성입니다.

이 튜토리얼에서는 innerHTML 속성을 사용합니다. 하지만 위의 방법을 배우면 DOM 트리 구조와 탐색을 이해하는 데 도움이 됩니다.

핫 AI 도구
Undress AI Tool
Undress AI Tool

무료로 이미지를 벗다

AI Clothes Remover
AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undresser.AI Undress
Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

Stock Market GPT
Stock Market GPT

더 현명한 결정을 위한 AI 기반 투자 연구

인기 있는 도구
메모장++7.3.1
메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6
드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전
SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)