沒有document.getElementByName方法
更新時間:2013年08月19日 18:02:29 作者:
document.getElementByName方法沒有document.getElementsByName得到的是標簽的數(shù)組,下面為大家詳細介紹下具體的使用,感興趣的朋友可以參考下,希望對大家有所幫助
首先聲明的是:
document.getElementByName方法沒有。document.getElementsByName得到的是標簽的數(shù)組
document.getElementId得到的是某一個標簽
<form name="form_write">
<input name="content" type="text">
然而可以用很淺顯的方式得到如:
var fn = document.getElementsByName("form_write")[0]; //得到這個form下的對象
fn.content.value;//就直接去用這個對象取值就可以了。
document.getElementById 1、getElementById
作用:一般頁面里ID是唯一的,用于準備定位一個元素
語法: document.getElementById(id)
參數(shù):id :必選項為字符串(String)
返回值:對象; 返回相同id對象中的第一個,按在頁面中出現(xiàn)的次序,如果無符合條件的對象,則返回 null
example:
document.getElementById("id1").value;
2、getElementsByName
作用:按元素的名稱查找,返回一個同名元素的數(shù)組
語法: document.getElementsByName(name)
參數(shù):name :必選項為字符串(String)
返回值:數(shù)組對象; 如果無符合條件的對象,則返回空數(shù)組,按在頁面中出現(xiàn)的次序
注意:返回數(shù)組值為value屬性的值,
如果某標簽無value屬性,當你添加上value屬性并賦值后,getElementsByName也能取到其值,
當未對value屬性賦值時, getElementsByName返回數(shù)組值將是undefined ,
但仍能獲得相同name標簽的個數(shù)document.getElementsByName(name).length
當未設(shè)置name屬性時document.getElementsByName仍能使用,它將根據(jù)你id取得value屬性的值
example:
document.getElementsByName("name1")[0].value;
document.getElementsByName("name1")[1].value;
<span id='CBylawIndexName' class='normalNode' value='all' >全部</span>"
<span id='CBylawIndexName' class='normalNode' value='ALL' >全部</span>"
span標簽其實沒有name和value屬性
但document.getElementsByName("CBylawIndexName")仍將取得value的值
3、getElementsByTagName
作用:按HTML標簽名查詢,返回一個相同標簽元素的數(shù)組
語法: object.getElementsByTagName(tagname) object可以是document或event.srcElement.parentElement等
參數(shù):tagname:必選項為字符串(String),根據(jù)HTML標簽檢索。
返回值:數(shù)組對象; 如果無符合條件的對象,則返回空數(shù)組,按在頁面中出現(xiàn)的次序
example:
document.getElementsByTagName("p")[0].childNodes[0].nodeValue;
document.getElementsByTagName("p")[1].childNodes[0].nodeValue;
document.getElementByName方法沒有。document.getElementsByName得到的是標簽的數(shù)組
document.getElementId得到的是某一個標簽
<form name="form_write">
<input name="content" type="text">
然而可以用很淺顯的方式得到如:
var fn = document.getElementsByName("form_write")[0]; //得到這個form下的對象
fn.content.value;//就直接去用這個對象取值就可以了。
document.getElementById 1、getElementById
作用:一般頁面里ID是唯一的,用于準備定位一個元素
語法: document.getElementById(id)
參數(shù):id :必選項為字符串(String)
返回值:對象; 返回相同id對象中的第一個,按在頁面中出現(xiàn)的次序,如果無符合條件的對象,則返回 null
example:
復制代碼 代碼如下:
document.getElementById("id1").value;
2、getElementsByName
作用:按元素的名稱查找,返回一個同名元素的數(shù)組
語法: document.getElementsByName(name)
參數(shù):name :必選項為字符串(String)
返回值:數(shù)組對象; 如果無符合條件的對象,則返回空數(shù)組,按在頁面中出現(xiàn)的次序
注意:返回數(shù)組值為value屬性的值,
如果某標簽無value屬性,當你添加上value屬性并賦值后,getElementsByName也能取到其值,
當未對value屬性賦值時, getElementsByName返回數(shù)組值將是undefined ,
但仍能獲得相同name標簽的個數(shù)document.getElementsByName(name).length
當未設(shè)置name屬性時document.getElementsByName仍能使用,它將根據(jù)你id取得value屬性的值
example:
復制代碼 代碼如下:
document.getElementsByName("name1")[0].value;
document.getElementsByName("name1")[1].value;
<span id='CBylawIndexName' class='normalNode' value='all' >全部</span>"
<span id='CBylawIndexName' class='normalNode' value='ALL' >全部</span>"
span標簽其實沒有name和value屬性
但document.getElementsByName("CBylawIndexName")仍將取得value的值
3、getElementsByTagName
作用:按HTML標簽名查詢,返回一個相同標簽元素的數(shù)組
語法: object.getElementsByTagName(tagname) object可以是document或event.srcElement.parentElement等
參數(shù):tagname:必選項為字符串(String),根據(jù)HTML標簽檢索。
返回值:數(shù)組對象; 如果無符合條件的對象,則返回空數(shù)組,按在頁面中出現(xiàn)的次序
example:
復制代碼 代碼如下:
document.getElementsByTagName("p")[0].childNodes[0].nodeValue;
document.getElementsByTagName("p")[1].childNodes[0].nodeValue;
相關(guān)文章
JavaScript、tab切換完整版(自動切換、鼠標移入停止、移開運行)
這篇文章主要介紹了JavaScript、tab切換完整版(自動切換、鼠標移入停止、移開運行)的相關(guān)資料,需要的朋友可以參考下2016-01-01JavaScript格式化日期時間的方法和自定義格式化函數(shù)示例
JavaScript默認的時間格式我們一般情況下不會用,所以需要進行格式化,下面說說我總結(jié)的JavaScript時間格式化方法2014-04-04