1.getElementById
作用:一般頁面里ID是唯一的,用于準備定為一個元素
語法: document.getElementById(id)
參數:id :必選項為字符串(String)
返回值:對象; 返回相同id對象中的第一個,按在頁面中出現的次序,如果無符合條件的對象,則返回 null
代碼
- example:document.getElementById("id1").value;
2.getElementsByName
作用:按元素的名稱查找,返回一個同名元素的數組
語法: document.getElementsByName(name)
參數:name :必選項為字符串(String)
返回值:數組對象; 如果無符合條件的對象,則返回空數組,按在頁面中出現的次序
代碼
- example:document.getElementsByName("name1")[0].value;
- document.getElementsByName("name1")[1].value;
3.getElementsByTagName
作用:按HTML標簽名查詢,返回一個相同標簽元素的數組
語法: object.getElementsByTagName(tagname) object可以是document或event.srcElement.parentElement等
參數:tagname:必選項為字符串(String),根據HTML標簽檢索。
返回值:數組對象; 如果無符合條件的對象,則返回空數組,按在頁面中出現的次序
代碼
- example:document.getElementsByTagName("p")[0].childNodes[0].nodeValue;
- document.getElementsByTagName("p")[1].childNodes[0].nodeValue