zoukankan      html  css  js  c++  java
  • 对属性特性操作的三个主要方法(创建,设置,删除)

    每个元素都有一个或多个特性,这些特性给相应元素或内容附加信息。操作特性的DOM的方法主要有三种

    分别是getAttribute()、setAttribute()、removeAttribute().

    getAttribute()方法主要用于获取属性值

    任何元素的所有属性,也都可以通过getAttirbute方法来获取器属性值。

    例如:

    在HTML中:<div id="mydiv" class="class" mychakk="mychakk" title="mytitle"></div>

    js代码:var div =  document.getelementbyid('mydiv");

                 alert(div.getAttribute("class"));           //class

                 alert(div.getAttribute("mychakk"));              //mychakk

                 alert(div.getAttribute("title"));               //mytitle

    同样,任何属性也可以通过DOM元素本身的属性来访问自己的属性,但它只能访问公认的属性。

    例如:

    在HTML中:<div id="mydiv" align="left" mychakk="mychakk"></div>

    js代码:   alert(div.id);       //mydiv

                   alert(div.align);         //left

                   alert(div.mychakk);           //undefined

    setAttribute方法用于设置元素的特性

    这个方法接受2个参数:要设置的特性名和值。

    如果特性已经存在,该特性会被特换;如果不存在,该特性会被创建。

    例如:

    在HTML中:       <div id="myid" ></div>

    js代码:     var  div = getelementbyid("mydiv");

                      div.setAttribute("id","someOtherid");

                      div.setAttribute("title","Some other text");

                      div.setAttribute("lang","fr");

    同样也可以像下面那样添加属性,其效果一样。

                   div.id="someOtherid"

                   div.align="left"

    removeAttribute方法用于删除元素的特性。

    调用这个方法可以清除特性的值,而且还可以从元素中彻底删除该特性。

    例如:

                div.removeAttribute("class");

  • 相关阅读:
    js 数组去重求和 (转载)
    表格插件汇总(转载)
    SQL Server 用一张表的数据更新另一张表的数据(转载)
    C#创建DataTable(转载)
    C# DataTable 和List之间相互转换的方法(转载)
    维度表,实体表,事实表之间的关系
    Scala中foldLeft的总结
    Scala集合Map
    从合并两个Map说开去
    UDAF(用户自定义聚合函数)求众数
  • 原文地址:https://www.cnblogs.com/binguo666/p/7826426.html
Copyright © 2011-2022 走看看