沒有document.getElementByName方法
更新時(shí)間:2013年08月19日 18:02:29 作者:
document.getElementByName方法沒有document.getElementsByName得到的是標(biāo)簽的數(shù)組,下面為大家詳細(xì)介紹下具體的使用,感興趣的朋友可以參考下,希望對大家有所幫助
首先聲明的是:
document.getElementByName方法沒有。document.getElementsByName得到的是標(biāo)簽的數(shù)組
document.getElementId得到的是某一個(gè)標(biāo)簽
<form name="form_write">
<input name="content" type="text">
然而可以用很淺顯的方式得到如:
var fn = document.getElementsByName("form_write")[0]; //得到這個(gè)form下的對象
fn.content.value;//就直接去用這個(gè)對象取值就可以了。
document.getElementById 1、getElementById
作用:一般頁面里ID是唯一的,用于準(zhǔn)備定位一個(gè)元素
語法: document.getElementById(id)
參數(shù):id :必選項(xiàng)為字符串(String)
返回值:對象; 返回相同id對象中的第一個(gè),按在頁面中出現(xiàn)的次序,如果無符合條件的對象,則返回 null
example:
document.getElementById("id1").value;
2、getElementsByName
作用:按元素的名稱查找,返回一個(gè)同名元素的數(shù)組
語法: document.getElementsByName(name)
參數(shù):name :必選項(xiàng)為字符串(String)
返回值:數(shù)組對象; 如果無符合條件的對象,則返回空數(shù)組,按在頁面中出現(xiàn)的次序
注意:返回?cái)?shù)組值為value屬性的值,
如果某標(biāo)簽無value屬性,當(dāng)你添加上value屬性并賦值后,getElementsByName也能取到其值,
當(dāng)未對value屬性賦值時(shí), getElementsByName返回?cái)?shù)組值將是undefined ,
但仍能獲得相同name標(biāo)簽的個(gè)數(shù)document.getElementsByName(name).length
當(dāng)未設(shè)置name屬性時(shí)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標(biāo)簽其實(shí)沒有name和value屬性
但document.getElementsByName("CBylawIndexName")仍將取得value的值
3、getElementsByTagName
作用:按HTML標(biāo)簽名查詢,返回一個(gè)相同標(biāo)簽元素的數(shù)組
語法: object.getElementsByTagName(tagname) object可以是document或event.srcElement.parentElement等
參數(shù):tagname:必選項(xiàng)為字符串(String),根據(jù)HTML標(biāo)簽檢索。
返回值:數(shù)組對象; 如果無符合條件的對象,則返回空數(shù)組,按在頁面中出現(xiàn)的次序
example:
document.getElementsByTagName("p")[0].childNodes[0].nodeValue;
document.getElementsByTagName("p")[1].childNodes[0].nodeValue;
document.getElementByName方法沒有。document.getElementsByName得到的是標(biāo)簽的數(shù)組
document.getElementId得到的是某一個(gè)標(biāo)簽
<form name="form_write">
<input name="content" type="text">
然而可以用很淺顯的方式得到如:
var fn = document.getElementsByName("form_write")[0]; //得到這個(gè)form下的對象
fn.content.value;//就直接去用這個(gè)對象取值就可以了。
document.getElementById 1、getElementById
作用:一般頁面里ID是唯一的,用于準(zhǔn)備定位一個(gè)元素
語法: document.getElementById(id)
參數(shù):id :必選項(xiàng)為字符串(String)
返回值:對象; 返回相同id對象中的第一個(gè),按在頁面中出現(xiàn)的次序,如果無符合條件的對象,則返回 null
example:
復(fù)制代碼 代碼如下:
document.getElementById("id1").value;
2、getElementsByName
作用:按元素的名稱查找,返回一個(gè)同名元素的數(shù)組
語法: document.getElementsByName(name)
參數(shù):name :必選項(xiàng)為字符串(String)
返回值:數(shù)組對象; 如果無符合條件的對象,則返回空數(shù)組,按在頁面中出現(xiàn)的次序
注意:返回?cái)?shù)組值為value屬性的值,
如果某標(biāo)簽無value屬性,當(dāng)你添加上value屬性并賦值后,getElementsByName也能取到其值,
當(dāng)未對value屬性賦值時(shí), getElementsByName返回?cái)?shù)組值將是undefined ,
但仍能獲得相同name標(biāo)簽的個(gè)數(shù)document.getElementsByName(name).length
當(dāng)未設(shè)置name屬性時(shí)document.getElementsByName仍能使用,它將根據(jù)你id取得value屬性的值
example:
復(fù)制代碼 代碼如下:
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標(biāo)簽其實(shí)沒有name和value屬性
但document.getElementsByName("CBylawIndexName")仍將取得value的值
3、getElementsByTagName
作用:按HTML標(biāo)簽名查詢,返回一個(gè)相同標(biāo)簽元素的數(shù)組
語法: object.getElementsByTagName(tagname) object可以是document或event.srcElement.parentElement等
參數(shù):tagname:必選項(xiàng)為字符串(String),根據(jù)HTML標(biāo)簽檢索。
返回值:數(shù)組對象; 如果無符合條件的對象,則返回空數(shù)組,按在頁面中出現(xiàn)的次序
example:
復(fù)制代碼 代碼如下:
document.getElementsByTagName("p")[0].childNodes[0].nodeValue;
document.getElementsByTagName("p")[1].childNodes[0].nodeValue;
相關(guān)文章
es6學(xué)習(xí)筆記之Async函數(shù)的使用示例
async 函數(shù),使得異步操作變得更加方便。它是 Generator 函數(shù)的語法糖。下面這篇文章主要給大家介紹了es6學(xué)習(xí)筆記之Async函數(shù)使用的相關(guān)資料,文中給出了詳細(xì)的示例代碼,需要的朋友們下面來一起看看吧。2017-05-05手把手教你用Javascript實(shí)現(xiàn)觀察者模式
這篇文章主要為大家介紹了Javascript觀察者模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-12-12JavaScript、tab切換完整版(自動切換、鼠標(biāo)移入停止、移開運(yùn)行)
這篇文章主要介紹了JavaScript、tab切換完整版(自動切換、鼠標(biāo)移入停止、移開運(yùn)行)的相關(guān)資料,需要的朋友可以參考下2016-01-01JavaScript格式化日期時(shí)間的方法和自定義格式化函數(shù)示例
JavaScript默認(rèn)的時(shí)間格式我們一般情況下不會用,所以需要進(jìn)行格式化,下面說說我總結(jié)的JavaScript時(shí)間格式化方法2014-04-04JS正則表達(dá)式驗(yàn)證賬號、手機(jī)號、電話和郵箱是否合法
這篇文章主要介紹了JS正則表達(dá)式驗(yàn)證賬號、手機(jī)號、電話和郵箱是否合法,需要的朋友可以參考下2017-03-03