沒有document.getElementByName方法

來源:互聯網
上載者:User

首先聲明的是:
document.getElementByName方法沒有。document.getElementsByName得到的是標籤的數組
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)
參數: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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.