获取表单对象,得三种方法getElementById(), getElementsByName(), and getElementsByTagName() 和用法...
? document.表單名稱.對象名稱.屬性值?? 這種方式IE支持 第三方的可能不支持,建議使用 getElementById(), getElementsByName(), and getElementsByTagName() 這幾個方
法,需要注意得是getElementsByName()取出得是個數組.
網上,我查找了相關得資料,提供出來供大家參考:
Most third-party browsers are “strict standards” implementations, meaning that they implement W3C and ECMA standards and ignore most of the proprietary
object models of Internet Explorer and Netscape.If the demographic for your Web site includes users likely to use less common browsers, such as Linux
aficionados, it might be a good idea to avoid IE-specific features and use the W3C DOM instead. by Internet Explorer 6, we see that IE implements
significant portions of the W3C DOM.
這段話的意思是大多數第三方瀏覽器只支持W3C的DOM,如果你的網站用戶使用其他的瀏覽器,那么你最好避免使用IE的私有屬性。而且IE6也開始支持W3C DOM。
畢竟大多數人還不了解標準,在使用標準前,你還可以在你的網頁中用document.all[]訪問文檔對象前面寫到WEB標準,今天繼續WEB標準下可以通過getElementById(),
getElementsByName(), and getElementsByTagName()訪問DOCUMENT中的任一個標簽:
1、getElementById()
getElementById()可以訪問DOCUMENT中的某一特定元素,顧名思義,就是通過ID來取得元素,所以只能訪問設置了ID的元素。
比如說有一個DIV的ID為docid:
?程序代碼
<div id="docid"></div>
那么就可以用getElementById("docid")來獲得這個元素。
2、getElementsByName()
這個是通過NAME來獲得元素,但不知大家注意沒有,這個是GET ELEMENTS,復數ELEMENTS代表獲得的不是一個元素,為什么呢?
因為DOCUMENT中每一個元素的ID是唯一的,但NAME卻可以重復。打個比喻就像人的身份證號是唯一的(理論上,雖然現實中有重復),但名字重復的卻很多。如果一個文檔
中有兩個以上的標簽NAME相同,那么getElementsByName()就可以取得這些元素組成一個數組。
比如有兩個DIV:
?程序代碼
<div name="docname" id="docid1"></div>
<div name="docname" id="docid2"></div>
那么可以用getElementsByName("docname")獲得這兩個DIV,用getElementsByName("docname")[0]訪問第一個DIV,用getElementsByName("docname")[1]訪問第二個DIV。
3、getElementsByTagName()
這個呢就是通過TAGNAME(標簽名稱)來獲得元素,一個DOCUMENT中當然會有相同的標簽,所以這個方法也是取得一個數組。
下面這個例子有兩個DIV,可以用getElementsByTagName("div")來訪問它們,用getElementsByTagName("div")[0]訪問第一個DIV,用
getElementsByTagName("div")[1]訪問第二個DIV。
總結一下標準DOM,訪問某一特定元素盡量用標準的getElementById(),訪問標簽用標準的getElementByTagName(),但IE不支持getElementsByName(),所以就要避免使用
getElementsByName(),但getElementsByName()和不符合標準的document.all[]也不是全無是處,它們有自己的方便之處,用不用那就看網站的用戶使用什么瀏覽器,由你自己
決定了。
?
總結
以上是生活随笔為你收集整理的获取表单对象,得三种方法getElementById(), getElementsByName(), and getElementsByTagName() 和用法...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win 08 子域的搭建(二):父子域和
- 下一篇: Flash开发的基本概念