• document.getElementById详解


    document.getElementById详解<转载>
    2009-08-21 16:21

    document.getElementById(" ") 得到的是一个对象,用 alert 显示得到的是“ object ”,而不是具体的值,它有 value 和 length 等属性,加上 .value 得到的才 是具体的值!


    参考资料:

    1. document . getElementById 的用法和 DHTML.CHM 的下载地址

    今天在网络上查找 document . getElementById 的用法,如下:

    A :      语法:

         oElement = document . getElementById ( sID )

          参数:

         sID   : 必选项。字符串 (String) 。

          返回值:

         oElement   : 对象 (Element) 。

          说明:

          根据指定的 id 属性值得到对象。返回 id 属性值等于 sID 的第一个对象的引用。假如对应的为一组对象       ,则返回该组对象中的第一个。

          如果无符合条件的对象,则返回 null 。

    B :有一个例子可以很好的说明:

       ****************************************************************************

       *                                                                           *

       *   这个函数中最关键的地方是 document.getElementById ,他是什么呢?        *

       *                                                                           *

       *   关于 document.getElementById ,是这样的:                              *

       *                                                                     *

       *   如: document.getElementById('hdrPageHeader_lblTitle')               *

       *                                                                     *

       *   表示的意思是:获取 ID 为 :hdrPageHeader_lblTitle 的对象                  *

       *                                                                           *

       * <a >aa</a>                              *

       * <a >bb</a>                               *

       * <a >cc</a>                              *

       * <script language="javascript">                                      *

       * <!--                                                                *

       * var idtext=document.getElementById('hdrPageHeader_lblTitle')        *

       * alert(idtext.innerText)                                             *

       * //-->                                                               *

       * </script>                                                           *

    C :另外还得到一个细节:

    ① document.getElementById 有时会抓 name 放过了 id ,据说是 IE 的一个 BUG ;

    页面中有

         <input type="hidden" value="2" />

              <select     >

    一个是 一个是

    用 document.getElementById 取第二个,可是,取到的却是第一个 >

    在 IE 中 getElementById 竟然不是先抓 id 而是先找 name 相同的物件 ...

    兩個 form, 每個 form 有兩個 textbox, 兩個 form 中的 textbox 是相同的 name, 但 id 都不同 ...

    這樣在 Firefox 是沒問題的 ... 但在 IE 卻只抓得到第一個出現的 name 資料

    2. document 对象

    http://www.phpx.com/man/dhtmlcn/objects/obj_document.html

    属性: title ; bgColor ; url; ( 使用: document.title)

    方法:
    focus

    使得元素得到焦点并执行由 onfocus 事件指定的代码。

    getElementById

    获取对 ID 标签属性为指定值的第一个对象的引用。

    getElementsByName

    根据 NAME 标签属性的值获取对象的集合。

    getElementsByTagName

    获取基于指定元素名称的对象集合。

    方法:
    focus

    使得元素得到焦点并执行由 onfocus 事件指定的代码。

    getElementById

    获取对 ID 标签属性为指定值的第一个对象的引用。

    getElementsByName

    根据 NAME 标签属性的值获取对象的集合。

    getElementsByTagName

    获取基于指定元素名称的对象集合。

  • 相关阅读:
    CodeForces Gym 100500A A. Poetry Challenge DFS
    CDOJ 486 Good Morning 傻逼题
    CDOJ 483 Data Structure Problem DFS
    CDOJ 482 Charitable Exchange bfs
    CDOJ 481 Apparent Magnitude 水题
    Codeforces Gym 100637G G. #TheDress 暴力
    Gym 100637F F. The Pool for Lucky Ones 暴力
    Codeforces Gym 100637B B. Lunch 找规律
    Codeforces Gym 100637A A. Nano alarm-clocks 前缀和
    TC SRM 663 div2 B AABB 逆推
  • 原文地址:https://www.cnblogs.com/HughTan/p/1573827.html
Copyright © 2020-2023  润新知