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