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 不包含空白节点 非标准文本,但是兼容性很好

  • 相关阅读:
    nodeJS实现完整文件夹结构压缩
    chrome浏览器插件开发
    让用户端JS触发F11全屏
    inline-block和float的共性和区别
    安家落户
    ActiveMQ简单实现之一对一生产和消费
    Centos下 修改mysql密码
    虚拟机centos7 安装was和ihs
    webservice简单实现
    Centos7安装mysql
  • 原文地址:https://www.cnblogs.com/xishaonian/p/6937764.html
Copyright © 2011-2022 走看看