var header = document.getElementById("header"); var output = document.getElementById("output"); function accessValue() { var content = header.firstChild.nodeValue; output.firstChild.nodeValue = "nodeValue로 접근 : " + content; } function accessHTML() { var content = header.innerHTML; output.innerHTML = "innerHTML로 접근 : " + content; }
<h1 id="header">Document Heading</h1> <button type="button" onclick="accessValue()">nodeValue로 접근</button> <button type="button" onclick="accessHTML()">innerHTML로 접근</button> <p>두 가지 방법이 동일합니다.</p> <div id="output"> </div>