Computer >> Máy Tính >  >> Lập trình >> Javascript

Làm cách nào để truy cập thuộc tính tài liệu bằng phương pháp W3C DOM?


Mô hình đối tượng tài liệu IE4 (DOM) này được giới thiệu trong Phiên bản 4 của trình duyệt Internet Explorer của Microsoft. IE 5 và các phiên bản mới hơn bao gồm hỗ trợ cho hầu hết các tính năng cơ bản của W3C DOM.

Ví dụ

Để truy cập thuộc tính tài liệu bằng phương pháp W3C DOM, bạn có thể thử chạy mã sau -

<html>
   <head>
      <title> Document Title </title>
      <script type="text/javascript">
         <!--
            function myFunc() {
               var ret = document.getElementsByTagName("title");
               alert("Document Title : " + ret[0].text );

               var ret = document.getElementById("heading");
               alert(ret.innerHTML );
            }
         //-->
      </script>
   </head>
   <body>
      <h1 id="heading">This is main title</h1>
      <p>Click the following to see the result:</p>

      <form id="form1" name="FirstForm">
         <input type = "button" value = "Click Me" onclick = "myFunc();" />
         <input type = "button" value = "Cancel">
      </form>

      <form d="form2" name="SecondForm">
         <input type = "button" value = "Don't ClickMe"/>
      </form>
   </body>
</html>