JS之setAttribute和getAttribute
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);不仅可以得到元素默认属性值,还能得到自定义属性值
5.
var node = ele.getAttributeNode(attributeName)得到属性节点
<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的更多相关文章
- JavaScript常用的方法和函数(setAttribute和getAttribute )
仅记录学习的新知识和示例,无干货. 1.setAttribute和getAttribute (Attribute:属性) setAttribute:为元素添加指定的属性,并为其赋值: ...
- js中setAttribute 的兼容性
js中setAttribute 的兼容性class和className兼容方法: object.setAttribute("class","content") ...
- JS中setAttribute的兼容性问题(摘自leejersey)
class和className兼容方法: object.setAttribute("class","content") 在IE8.Chrome.火狐.Opera ...
- setAttribute()、getAttribute()与ele[attr]与自定义属性
一.自定义属性设置 1.setAttrbute() var q=document.getElementById("q"); q.setAttribute("index&q ...
- InvocationTargetException异常的深入研究-servlet的setAttribute与getAttribute
在某项目中,前端jsp传入的用户id数据通过session域传入后台servlet进行处理的过程中,无意间出现了InvocationTargetException异常 前端部分代码如下:测试代码,非原 ...
- setAttribute 和 getAttribute 的用法
setAttribute() 是用于设置自定义属性的方法,有两个参数,第一个是属性名,第二个是属性值, 添加时必须用引号括起来: 此时的box就加上了一个自定义属性名和属性值,可以根据需要赋取 g ...
- request.getParameter和request.setAttribute/request.getAttribute
https://blog.csdn.net/ryelqy/article/details/79230513 request.getQueryString https://blog.csdn.net/w ...
- 属性attribute和property的区别
<!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content ...
- jQuery笔记归纳
使用jQuery前首先需要导如jQuery包:https://jquery.com/(点击下载按钮,进入后点击鼠标右键另存为即可下载) 例如:<script type="te ...
随机推荐
- object-c 协议和委托
协议相当于接口 委托相当于帮助实现其它类的功能 object-c提供的协议机制,一个类可以实现多个协议,从而感觉上像多继承一样
- [Unity2D]预制件Prefab
预制件Prefab是一个组件模板,比如在游戏里面要实现开枪的时候会有子弹不停地从枪口飞出来,那么就可以通过Prefab来实现子弹的游戏对象,表示所有的子弹都是从同一个Prefab来构建出来的,也可以理 ...
- [BZOJ 3759]Hungergame
Nim游戏获胜的条件是所有石子的异或和为0 如果先手要获胜,那么一定是打开了一个异或和为0的极大子集 什么是极大子集呢? 就是无论后手打开任何子集的箱子,都不能再使此时打开的箱子异或和为0. 容易证明 ...
- Scrum会议3
组名称:天天向上 项目名称:连连看 参会成员:王森(Master)张金生 张政 栾骄阳 时间:2016.10.18 已完成内容: 1.GUI布局设计 2.通过在网上大量阅览代码,大体了解连连看游戏制作 ...
- MAC环境下 nodejs 4.5.0 卸载
MAC 环境下,我之前的删除 node 环境如果是用brew 安装的 node.js 4.4 之前的版本.可以网上参考其他方法,我之前的帖子也有之前的删除方法.参考帖子: http://www.cnb ...
- gitlab & gerrit & git & repo & jenkins
Omnibus GitLab documentation(中文安装说明) 在自己的服务器上部署 GitLab 社区版->较为全面 GIT & REPO & GERRIT (三) ...
- linux load average
性能分析_linux服务器CPU_Load Average 理解Linux系统中的load average(图文版) 理解Load Average做好压力测试 top命令的Load average 含 ...
- DS实验题 Floyd最短路径 & Prim最小生成树
题目: 提示: Floyd最短路径算法实现(未测试): // // main.cpp // Alg_Floyd_playgame // // Created by wasdns on 16/11/19 ...
- pycharm 皮肤主题及个性化设置
1.设置IDE皮肤主题 File -> Settings -> IDE Settings -> Appearance -> Theme -> 选择“Alloy.IDEA ...
- 【翻译】CEDCE2010 制作魅力绘制而要知道的光学小知识
关于Silicon Studio 个人觉得他们的后处理技术在国际上还是有相对水准的,而且不少日系游戏也采用了他们的全平台YEBIS 3的中间件. YEBIS 3的特性可以看下这个 http:// ...