zoukankan      html  css  js  c++  java
  • JavaScript 内容串联 ---Document

    Document 对象

    一、找到对象
    document.getElementById("id");//根据id找,最多找出一个,因为ID是唯一的;
    document.getELementsByName("name");//根据name找,找出来的是数组;
    document.getElementsByTagName("div");//根据标签名找,找出来的是数组;

    例:

    var a=document.getElementsByTagName("div");
    for(var i=0;i<a.length;i++)
    {
        a[i].style.border="5px solid red";
    }

    var a=document.getElementById("dd");
    a.style.height="200px";
    a.style.backgroundColor="blue";

    二、操作属性

    1.获取属性值

    var a=document.getElementById("text");

    var s=a.getAttribute("daan");

    2.添加或修改属性值

    var a=document.getElementById("bnt");

    a.setAttribute("属性名","属性值"); -------设置一个属性,添加或者更改。

    3.删除属性

    var a=document.getElementById("bnt");

    a.removeAttribute("属性名");------------移除一个属性

    三、操作样式

    (一)操作内联样式

    var a=document.getElementById("bnt");

    a.style.XXXX=值;

    (二)操作样式表的class选择器

    var b=document.getElementById("bnt");

    b.className="dd";

    或者

    b.setAttribute("class","dd");

    例:设置奇偶行不同背景色。

    第一步:做HTML标记

    第二步:做样式

    第三步:用JS代码为元素加上样式选择器

    例:<style type="text/css">
    #d
    {
        height:20px;
        border-bottom:#000 2px solid;
    }
    .odd
    {
         background-color:red;
    }
    .even
    {
         background-color:blue;
    }
    </style>
    </head>

    <body>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    </body>

    <script language="javascript">
    var d=document.getElementsByTagName("div");
    for(var i=0;i<d.length;i++)
    {
        d[i].setAttribute("id","d");
        if(i%2==0)
        {
            d[i].className="odd";
        }
        else
        {
           d[i].className="even";
        }
    }
    </script>

    四、操作内容

    五、操作元素

  • 相关阅读:
    Go 语言简介(下)— 特性
    Array.length vs Array.prototype.length
    【转】javascript Object使用Array的方法
    【转】大话程序猿眼里的高并发架构
    【转】The magic behind array length property
    【转】Build Your own Simplified AngularJS in 200 Lines of JavaScript
    【转】在 2016 年做 PHP 开发是一种什么样的体验?(一)
    【转】大话程序猿眼里的高并发
    php通过token验证表单重复提交
    windows 杀进程软件
  • 原文地址:https://www.cnblogs.com/likaixuan/p/4445977.html
Copyright © 2011-2022 走看看