获取元素属性

//获取元素行间属性

elem.getAttribute('id');
oImg.src; //obj.src 浏览器不同路径解析不同弄个 //设置元素行间属性 elem.setAttribute('index','2'); //删除元素行间属性 elem.removeAttribute('index');

//IE中使用的是obj.currentStyle方法,而FF是用的是getComputedStyle 方法
//下面这个函数,能够获取一个元素的任意 CSS 属性值。IE不支持getComputedStyle()方法,getComputedStyle 仅支持读并不支持写,返回的对象的键名是 css 的驼峰式写法
// IE中使用的是obj.currentStyle方法,而FF是用的是getComputedStyle 方法
//即使computedStyle.border不会在所有浏览器中都返回值,但computedStyle.borderLeftWidth则会返回值。
// obj.style:这个方法只能JS只能获取写在html标签中的写在style属性中的值(style=”…”),而无法获取定义在<style type="text/css">里面的属性。
//根据 《JavaScript 高级程序》所描述的情况 ,float 是 JS 的保留关键字。根据 DOM2 级的规范,取元素的 float 的时候应该使用 cssFloat。其实 chrome 和 Firefox 是支持 float 属性的,也就是说可以直接使用。
function getStyle(element, attr) {
if(element.currentStyle) {
return element.currentStyle[attr];
} else {
return getComputedStyle(element, false)[attr];
}
}

 

posted @ 2019-03-04 15:33  nohuman  阅读(444)  评论(0编辑  收藏  举报