프로젝트4 - div 섹션 구역 분리 와 CSS 스타일 적용하기

이미지
웹 브라우저에 표시되는 글자와 이미지 들에 디자인 적인 효과( 배경색, 글자색, 크기 조절...) 및 브라우저 내에서의 배치등을 조절하기 위해서는 구역을 나누면 편리해진다. 바로 구역(섹션) 을 나누어 주는 역할을 하는 태그가 div 이다. <div>태그는 브라우저에서 보여지는 시각적 기능은 없다. 단지, 구역을 나누는 역할을 한다.  코드를 만들어 본다. <!DOCTYPE html> <html> <head>   <title>div</title> </head> <body>   <div>     <p>Ice cream is a frozen dessert typically made from milk or cream that has been flavoured with a sweetener,        either sugar or an alternative, and a spice,        such as cocoa or vanilla, or with fruit, such as strawberries or peaches.</p>   </div>   <div>     <p> Food colouring is sometimes added in addition to stabilizers.        mixture is cooled below the freezing point of water and stirred to incorporate air spaces and prevent detectable ice crystals from forming.        It can also be made by whisking a flavoured cream base and liquid nitrogen together.        The result is a smooth, semi-solid foam that is solid at v

자바스크립트로 HTML요소 콘텐츠 텍스트 수정하기 (textContent 속성)

 


자바스크립트로 HTML 요소의 콘텐츠 영역을 수정할 수 있다.
코드를 먼저 보자.
 <h1>Hi, Here is TextContent test.
    <span style="display:none">테스트 입니다.</span>
 </h1>

  <script>
    let text = document.getElementsByTagName("h1");
    console.log(text);
    console.log(`text is ${text[0].textContent}`);
  </script>
document.getElementsByTagName() 메서드로 h1 요소에 접근한다.
h1요소의 콘텐츠 부분의 텍스트를 수정하기 위함이다.
document.getElementsByTagName() 메서드 의 반환값은 

HTMLCollection [h1]

이므로, 바로 text.textContent 로 접근을 하면 undefined 가 나온다.
HTMLCollection의 첫번째 요소값의 textContent에 접근해야 한다.
그 코드가 바로, text[0].textContent 이다.

결과는 
text is Hi, Here is TextContent test. 테스트 입니다
로 나온다.

span 태그의 style 속성에서 화면에 보이지 않도록 display:none 으로 설정했지만
여기서는 요소의 텍스트가 다 나온다.
바로 textContent 의 성질은 요소가 가지고 있는 모든 텍스트를 반환한다는 것 이다.
추가로, 
 text[0].textContent = "수정함";
코드를 삽입하면, 브라우저 화면에는 

수정함


수정한 텍스트가 출력이 된다.
요소.textContent 속성은 요소가 가진 모든 텍스트를 반환 및 수정이 가능하다. 

댓글

이 블로그의 인기 게시물

구글 블로그 이미지 복사 붙여넣기 가능한가요?

HTML 페이지 기본 구성.(HTML Page Structure)

구글 블로그 대표이미지 설정하기