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. BZOJ 1067 & Interval_Tree

    1067: [SCOI2007]降雨量 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 3099 Solved: 800 Description 我们常常 ...

  2. Android 第三方

    把优酷.土豆等取出它们真是的视频播放地址:http://vparser.com/ volley 项目地址 https://github.com/smanikandan14/Volley-demo (1 ...

  3. topcoder 594 DIV2 foxandclassroom

    暴力枚举 1 #include <iostream> #include <vector> #include <string> using namespace std ...

  4. 【BZOJ】1036: [ZJOI2008]树的统计Count(lct/树链剖分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1036 lct: (ps:为嘛我的那么慢T_T,不知道排到哪了..难道别人都是树剖吗...看来有必要学 ...

  5. BZOJ3772: 精神污染

    Description 兵库县位于日本列岛的中央位置,北临日本海,南面濑户内海直通太平洋,中央部位是森林和山地,与拥有关西机场的大阪府比邻而居,是关西地区面积最大的县,是集经济和文化于一体的一大地区, ...

  6. 原生js记住密码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 四、saltstack如何管理对象?

    实验前环境: [root@super65 ~]# salt-key -L[root@super65 ~]# salt-key -a super65.cn -y salt管理对象简介: saltstac ...

  8. Apache Spark源码走读之15 -- Standalone部署模式下的容错性分析

    欢迎转载,转载请注明出处,徽沪一郎. 概要 本文就standalone部署方式下的容错性问题做比较细致的分析,主要回答standalone部署方式下的包含哪些主要节点,当某一类节点出现问题时,系统是如 ...

  9. P4factory <Towards a better behavioral model: bmv2>

    尝试使用bmv2行为模型来跑p4实例. 原文说明: We have released a new version of the behavioral model, written in C++. So ...

  10. jquery()的三种$()

    jQuery中的$以及选择器总结 $号是jQuery”类”的一个别称,$()构造了一个jQuery对象.所以,”$()”可以看作jQuery的”构造函数”(个人观点). 一.$符号 1.$()可以是$ ...