Computer >> 컴퓨터 >  >> 프로그래밍 >> HTML

HTML DOM 테이블 createTFoot() 메서드 – 사용법과 예제 총정리

HTML DOM의 createTFoot() 메서드는 HTML 문서 내 테이블에 빈 <tfoot> 요소를 생성하여 추가하는 기능을 합니다. 이 메서드를 활용하면 자바스크립트만으로 동적으로 테이블 바닥글(footer) 영역을 만들 수 있습니다.

문법(Syntax)

createTFoot() 메서드의 기본 문법은 다음과 같습니다.

object.createTFoot()

이 메서드는 매개변수를 받지 않으며, 새로 생성된 <tfoot> 요소 객체를 반환합니다. 만약 테이블에 이미 tfoot 요소가 존재한다면, 새로 생성하지 않고 기존 요소를 그대로 반환한다는 점도 참고하세요.

예제(Example)

아래 예제는 버튼을 클릭하면 createTFoot() 메서드를 호출하여 테이블에 바닥글을 추가하는 코드입니다.

<!DOCTYPE html>
<html>
<style>
    body {
        color: #000;
        background: lightblue;
        height: 100vh;
        text-align: center;
    }
    table {
        margin: 2rem auto;
    }
    caption {
        color: #db133a;
    }
    .btn {
        background: #db133a;
        border: none;
        height: 2rem;
        border-radius: 2px;
        width: 40%;
        display: block;
        color: #fff;
        outline: none;
        cursor: pointer;
        margin: 1rem auto;
    }
    .show {
        font-size: 1.2rem;
    }
</style>
<body>
<h1>DOM Table createTFoot() Method Demo</h1>
<table border="2">
<tr>
<td>Name</td>
<td>Roll No.</td>
</tr>
<tr>
<td>John</td>
<td>071717</td>
</tr>
<tr>
<td>Jane</td>
<td>031717</td>
</tr>
</table>
<button onclick="create()" class="btn">Create Footer</button>
<script>
    function create() {
        var tableFooter = document.querySelector('table').createTFoot();
        tableFooter.innerHTML = "This is table footer";
    }
</script>
</body>
</html>

실행 결과(Output)

위 코드를 실행하면 학생 이름과 학번이 담긴 테이블과 'Create Footer' 버튼이 화면에 나타납니다.

HTML DOM 테이블 createTFoot() 메서드 – 사용법과 예제 총정리

버튼을 클릭하면 create() 함수가 실행되어 테이블 하단에 <tfoot> 요소가 생성되고, innerHTML 속성을 통해 "This is table footer"라는 텍스트가 삽입됩니다.

HTML DOM 테이블 createTFoot() 메서드 – 사용법과 예제 총정리

핵심 정리

  • createTFoot(): 테이블에 빈 tfoot 요소를 생성·추가하는 메서드
  • 기존 tfoot이 있으면 새로 만들지 않고 해당 요소를 반환
  • createTHead(), deleteTFoot() 등 다른 테이블 관련 DOM 메서드와 함께 활용 가능