Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> JavaScript

Cách truy cập thuộc tính Document bằng W3C DOM method

W3C DOM dùng các phương thức chuẩn: getElementById, getElementsByTagName, querySelector...

Ví dụ

<html>
  <head>
    <title> Document Title </title>
    <script>
      function myFunc() {
        var ret = document.getElementsByTagName("title");
        alert("Document Title: " + ret[0].textContent); // textContent thay text
        var ret = document.getElementById("heading");
        alert(ret.innerHTML);
      }
    </script>
  </head>
  <body>
    <h1 id="heading">This is main title</h1>
    <p>Nhấn để xem kết quả:</p>
    <form id="form1" name="FirstForm">
      <input type="button" value="Click Me" onclick="myFunc();" />
      <input type="button" value="Cancel">
    </form>
    <form id="form2" name="SecondForm">
      <input type="button" value="Don't ClickMe" />
    </form>
  </body>
</html>