1.ele.getAttribute(attributeName);

返回元素的指定属性值,如果元素没有该属性,则返回null

2.ele.setAttribute(attributeName,value);

为元素指定属性设置值,如果没有该属性,则创建该属性,并赋值

3.在IE 7以及更早版本部分属性的设置应使用另外的名称,为了兼容IE

<script>
           dom=(function(){
           var fixAttr={
                  tabindex:'tabIndex',
                  readonly:'readOnly',
                  'for':'htmlFor',
                  'class':'className',
                  maxlength:'maxLength',
                  cellspacing:'cellSpacing',
                  cellpadding:'cellPadding',
                  rowspan:'rowSpan',
                  colspan:'colSpan',
                  usemap:'useMap',
                  frameborder:'frameBorder',
                  contenteditable:'contentEditable'
           },

           //模拟设置attribute,
          div=document.createElement('div');
          div.setAttribute('class','t');

          var supportSetAttr = div.className === 't';

           return {
                   setAttr:function(el, name, val){
                        el.setAttribute(supportSetAttr ? name : (fixAttr[name] || name), val);
                   },
                   getAttr:function(el, name){
                        return el.getAttribute(supportSetAttr ? name : (fixAttr[name] || name));
                    }
              }
        })();

        window.onload=function(){
               var mydiv=document.getElementById("d1");
               dom.setAttr(mydiv, 'class', 'bg');
        }
    </script>

在IE 7以及更早版本中,setAttribute('class','fo');能为元素添加class='fo'.

getAttribute('class');能返回fo值,但是为元素添加的类不起作用,应为IE 7及更早版本的类设置用className,而不是class

 4.

getAttribute(attributeName);不仅可以得到元素默认属性值,还能得到自定义属性值

ele.attributeName或者ele['attributeName']只能得到元素默认存在的属性值

5.

var node = ele.getAttributeNode(attributeName)得到属性节点

console.log(node);//name=value的形式
console.log(node.name);

console.log(node.value);
<body>
    <p id="p1" customData="pmx">ppp</p>
    <script>
       var p = document.getElementById("p1");
       var pnode = p.getAttributeNode("customData");
       console.log(pnode)
    </script>
</body>

6.ownerElement返回属性节点所附属的元素

语法:

  attrObj.ownerElement

 
 

JS之setAttribute和getAttribute的更多相关文章

  1. JavaScript常用的方法和函数(setAttribute和getAttribute )

    仅记录学习的新知识和示例,无干货. 1.setAttribute和getAttribute          (Attribute:属性) setAttribute:为元素添加指定的属性,并为其赋值: ...

  2. js中setAttribute 的兼容性

    js中setAttribute 的兼容性class和className兼容方法: object.setAttribute("class","content") ...

  3. JS中setAttribute的兼容性问题(摘自leejersey)

    class和className兼容方法: object.setAttribute("class","content") 在IE8.Chrome.火狐.Opera ...

  4. setAttribute()、getAttribute()与ele[attr]与自定义属性

    一.自定义属性设置 1.setAttrbute() var q=document.getElementById("q"); q.setAttribute("index&q ...

  5. InvocationTargetException异常的深入研究-servlet的setAttribute与getAttribute

    在某项目中,前端jsp传入的用户id数据通过session域传入后台servlet进行处理的过程中,无意间出现了InvocationTargetException异常 前端部分代码如下:测试代码,非原 ...

  6. setAttribute 和 getAttribute 的用法

    setAttribute()   是用于设置自定义属性的方法,有两个参数,第一个是属性名,第二个是属性值, 添加时必须用引号括起来: 此时的box就加上了一个自定义属性名和属性值,可以根据需要赋取 g ...

  7. request.getParameter和request.setAttribute/request.getAttribute

    https://blog.csdn.net/ryelqy/article/details/79230513 request.getQueryString https://blog.csdn.net/w ...

  8. 属性attribute和property的区别

    <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...

  9. jQuery笔记归纳

      使用jQuery前首先需要导如jQuery包:https://jquery.com/(点击下载按钮,进入后点击鼠标右键另存为即可下载)    例如:<script type="te ...

随机推荐

  1. Wikioi 1020 孪生蜘蛛 Label:Floyd最短路

    题目描述 Description 在G城保卫战中,超级孪生蜘蛛Phantom001和Phantom002作为第三层防卫被派往守护内城南端一带极为隐秘的通道. 根据防护中心的消息,敌方已经有一只特种飞蛾 ...

  2. java不用jni,也可以获得当前系统性能信息

    最近做个项目,就是要取得cpu占有率等等的系统信息,一开始以为要用动态链接库了,但后来发现可以像下面这样做,不去调用jni,这样省去了很多看新技术的时间o(∩_∩)o... 在Java中,可以获得总的 ...

  3. 将java的class文件放到一个指定文件夹下

    用javac执行java文件时,要把java文件的class文件放到指定文件夹下,注意文件夹要创建好,执行javac -d 文件夹 ***.java 如图: 在class文件夹下就出现了L的class ...

  4. 一些开发遇到的"小问题",你能答对多少?

    我会把问题先写在前面,答案用白色字体写在后面.所以用鼠标选择文本就可以看到答案啦. 调用await后因为切换了线程环境(这种说法可能不严谨,但我只能想到这种说法),httpcontext会为null. ...

  5. IOS第七天(1:UiTableView 的基本用法)

    ***表格控件 #import "HMViewController.h" @interface HMViewController () <UITableViewDataSou ...

  6. Leetcode | Parentheses 相关

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  7. 连连看的设计与实现——四人小组项目(GUI)

    项目GUI界面经过简单设计,整理如下:(图片截取致宠物连连看3.1) 点开游戏后界面显示: 点击菜单游戏—>初级 后显示 -------------------------- > 当游戏时 ...

  8. explicit关键字

    C++中,explicit关键字用来修饰类只有一个参数的构造函数,被修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换. explicit使用注意事项: explicit 关 ...

  9. c#String的不变特性,可读但不可写性

    谈到字符串,大家自然觉得简单,但是总是有一些小的问题隐约出现,下面我就系统的说一下字符串的问题,有说不到日后再予补充. 1,首先String是一个类,string只是String类的一个别名,别名的意 ...

  10. Markdown基本用法

    Markdown基本用法 不同的编辑器对本文中的功能可能显示情况不一样,马克飞象可以完美支持和显示. 一.标题 h1标题 #h1标题 h1标题 h1标题 == h2标题 ##h2标题 h2标题 h2标 ...