zoukankan      html  css  js  c++  java
  • 找对象

    00x1 根据表单来找对象:window.document.getElementsByName("表单名");

    //表单元素,可以用name查询,返回的是对象的集合.类型为数组  

    <html>
    <head><title>找对象案例</title></head>
    <script>
    function danji()
    {
    alert(window.document.getElementsByName('biaodan')[0].value);
    }
    </script>
    <body>
    <input name="biaodan" type="text" value="hello world">
    <input type="submit" name="anniu" value="提交" onclick="danji();">
    </body>
    </html>

    00x2 根据ID来找对象:window.document.getElementById("id名");

    //通过ID寻找,返回类型是对象  

    •  window.documents.getElementByid("ID名");

    00x3 根据标签名来找对象:window.document.getElementsByTagName("标签名");

    //通过标签寻找,返回值是“对象的集合”,即使是一个对象,也会包装成集合  

    <html>
    <head><title>找对象案例</title></head>
    <script>
        function danji()
        {
            var data = window.document.getElementsByTagName("p");
            data[1].style.background="red"; //将获取到的第一个元素背景设置为红色
        }
    </script>
    <body>
        <p>hello world</p>
        <p>nihao shijie</p>
        <p>zhenxishaonianshi</p>
        <input type="submit" value="提交" onclick="danji();">
    </body>
    </html>

     以上三种是较为常用找对象的三种方法。

     

    //类名查找
    document.getElementsByClassName('test2')[0].style.background = 'red';

    //通过子节点
    alert(document.getElementById('test1').childNodes.length); //显示7 包含空白节点
    alert(document.getElementById('test1').children.length); //显示3 不包含空白节点 非标准文本,但是兼容性很好

  • 相关阅读:
    [Usaco2005 open]Expedition
    舞会
    双栈维护之--Hdu4699 editor
    利用两个堆来维护第K大之Poj3784 Running Median
    Zju1061Web Navigation 网络导航
    Qsort求静态的第K大
    BZOJ2726【SDOI2012】任务安排(斜率优化Dp+二分查找)
    P2365 任务安排 斜率优化入门
    任务处理--斜率优化Dp入门
    结构体排序教学
  • 原文地址:https://www.cnblogs.com/xishaonian/p/6937764.html
Copyright © 2011-2022 走看看