document.getElementById/Name/TagName


document.getElementById  

1、getElementById

作用:一般頁面里ID是唯一的,用於准備定位一個元素
語法: document.getElementById(id)
參數:id :必選項為字符串(String)
返回值:對象; 返回相同id對象中的第一個,按在頁面中出現的次序,如果無符合條件的對象,則返回 null

example:
document.getElementById("id1").value;

2、getElementsByName

作用:按元素的名稱查找,返回一個同名元素的數組
語法: document.getElementsByName(name)
參數:name :必選項為字符串(String)
返回值:數組對象; 如果無符合條件的對象,則返回空數組,按在頁面中出現的次序
注意:返回數組值為value屬性的值,
            如果某標簽無value屬性,當你添加上value屬性並賦值后,getElementsByName也能取到其值,
            當未對value屬性賦值時,  getElementsByName返回數組值將是undefined ,
            但仍能獲得相同name標簽的個數document.getElementsByName(name).length
            當未設置name屬性時document.getElementsByName仍能使用,它將根據你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標簽名查詢,返回一個相同標簽元素的數組
語法: 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM