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 ...
随机推荐
- td也可以溢出隐藏显示
或许我这篇文章一取这样的名字,就会有人要问了:你怎么还在关注table啊,那早就过时了…赶紧Xhtml…div好…ul好…ol好…dl好…完了,不知道还有什么好了. table真的过时了么?你真的了解 ...
- currentRowChanged 的注意事项
Qt中的表单控件QListWidget类提供了许多信号函数,可以和用户交互,其中有个currentRowChanged ( int currentRow ) 是检测当前选中行是否发生了改变,如果改变了 ...
- linux中执行命令权限不够怎样处理
在linux中执行命令权限不够就要增加权限,先看遇到的情况 查看权限情况 那就赋予权限 执行命令
- Ctrl+Shift+F12切换最大化编辑器
常用快捷键(keymaps:Default情况下) Esc键编辑器(从工具窗口) F1帮助千万别按,很卡! F2(Shift+F2)下/上高亮错误或警告快速定位 F3向下查找关键字出现位置 F4查找变 ...
- java---相亲练习
public class wu{ public static void main(String[] args){ //TODO 自动生成的方法存根 int a = 0,b = 0,c = 0; int ...
- jsp七大动作指令
jsp 七大动作指令 1) jsp:include 指令 用于在请求处理阶段包含来自一个Servlet或jsp页面的响应.和编译指令中的include不同,include只能用于包含静态页面,而jsp ...
- 管理Sass项目文件结构
构建你的结构体系 CSS预处理器的特点之一是可以把你的代码分割成很多个文件,而且不会影响性能.这都要归功于Sass的@import命令,只要在你的开发环境下,你调用不管多少文件,最终将编译出一个CSS ...
- 【转】SVN环境搭建教程
http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html http://www.cnblogs.com/xiaobaihom ...
- mongodb 3.2 分片部署步骤
#linux 网络优化1. 文件中/etc/sysctl.conf, 加入net.core.somaxconn = 2048fs.file-max = 2000000fs.nr_open = 2000 ...
- OpenGL EXT: shader_buffer_load
http://www.opengl.org/registry/specs/NV/shader_buffer_load.txt Overview At a very coarse level, GL h ...