document.getElementById詳解

來源:互聯網
上載者:User

document.getElementById(" ") 得到的是一個對象,用 alert 顯示得到的是“ object ”,而不是具體的值,它有 value 和 length 等屬性,加上 .value 得到的才 是具體的值!

參考資料:

1. document . getElementById 的用法和 DHTML.CHM 的

今天在網路上尋找 document . getElementById 的用法,如下:

A :      文法:

     oElement = document . getElementById ( sID )

      參數:

     sID   : 必選項。字串 (String) 。

      傳回值:

     oElement   : 對象 (Element) 。

      說明:

      根據指定的 id 屬性值得到對象。返回 id 屬性值等於 sID 的第一個對象的引用。假如對應的為一組對象       ,則返回該組對象中的第一個。

      如果無合格對象,則返回 null 。

 

B :有一個例子可以很好的說明:

 

   ****************************************************************************

   *                                                                           *

   *   這個函數中最關鍵的地方是 document.getElementById ,他是什麼呢?        *

   *                                                                           *

   *   關於 document.getElementById ,是這樣的:                              *

   *                                                                     *

   *   如: document.getElementById('hdrPageHeader_lblTitle')               *

   *                                                                     *

   *   表示的意思是:擷取 ID 為 :hdrPageHeader_lblTitle 的對象                  *

   *                                                                           *

   * <a >aa</a>                              *

   * <a >bb</a>                               *

   * <a >cc</a>                              *

   * <script language="javascript">                                      *

   * <!--                                                                *

   * var idtext=document.getElementById('hdrPageHeader_lblTitle')        *

   * alert(idtext.innerText)                                             *

   * //-->                                                               *

   * </script>                                                           *

 

 

C :另外還得到一個細節:

① document.getElementById 有時會抓 name 放過了 id ,據說是 IE 的一個 BUG ;

頁面中有

     <input type="hidden" value="2" />

          <select     >

一個是 一個是

用 document.getElementById 取第二個,可是,取到的卻是第一個 >

在 IE 中 getElementById 竟然不是先抓 id 而是先找 name 相同的物件 ...

兩個 form, 每個 form 有兩個 textbox, 兩個 form 中的 textbox 是相同的 name, 但 id 都不同 ...

這樣在 Firefox 是沒問題的 ... 但在 IE 卻只抓得到第一個出現的 name 資料

2. document 對象

http://www.phpx.com/man/dhtmlcn/objects/obj_document.html

屬性: title ; bgColor ; url; ( 使用: document.title)

方法:
focus

使得元素得到焦點並執行由 onfocus 事件指定的代碼。

getElementById

擷取對 ID 標籤屬性為指定值的第一個對象的引用。

getElementsByName

根據 NAME 標籤屬性的值擷取對象的集合。

getElementsByTagName

擷取基於指定元素名稱的對象集合。

方法:
focus

使得元素得到焦點並執行由 onfocus 事件指定的代碼。

getElementById

擷取對 ID 標籤屬性為指定值的第一個對象的引用。

getElementsByName

根據 NAME 標籤屬性的值擷取對象的集合。

getElementsByTagName

擷取基於指定元素名稱的對象集合。

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1897389

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.