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");

  • 相关阅读:
    学习进度条08
    学习进度条07
    子数组和最大值(二维)
    学习进度条06
    构建之法阅读笔记04
    四则运算网页版
    泛型代码中的默认关键字
    js 日期大小比较
    c#Reverse字符串
    c#获取数组中指定元素的索引
  • 原文地址:https://www.cnblogs.com/binguo666/p/7826426.html
Copyright © 2011-2022 走看看