05-JS特效-缓动动画-获取元素样式(行内、内嵌、外链)
2016-12-27 22:31
603 查看
火狐谷歌IE9+ : window.getComputedStyle(元素,null).width;
ie678: 元素.currentStyle.width;
getComputedStyle 和 currentStyle 获取到的都是对象
div.style[“width”] 变量赋值
window.getComputedStyle(div,null).width;
div.currentStyle[“width”]; 变量获取 (IE678)
window.getComputedStyle(div,null)[“width”];
参数1:获取属性的元素。
参数2:伪元素,C3学习。
ie678: 元素.currentStyle.width;
getComputedStyle 和 currentStyle 获取到的都是对象
属性值的获取:
只能获取行内式:(既能获取又能赋值)
div.style.width 单个赋值div.style[“width”] 变量赋值
获取任意类型的CSS样式的属性值:(只能获取)
div.currentStyle.width; 单个获取 (IE678)window.getComputedStyle(div,null).width;
div.currentStyle[“width”]; 变量获取 (IE678)
window.getComputedStyle(div,null)[“width”];
参数1:获取属性的元素。
参数2:伪元素,C3学习。
//这两个方法不管是行内式、内嵌式、外联式的样式,都可以获取(自己测试吧...) //火狐谷歌IE9+:获取样式方法;带单位 console.log(window.getComputedStyle(box,null).width);//200px;行内 console.log(window.getComputedStyle(box,null).height);//300px;外链 console.log(window.getComputedStyle(box,null).left);//100px;内嵌 //IE678:获取样式方法:带单位 console.log(box.currentStyle.width); console.log(box.currentStyle.height); console.log(box.currentStyle.left); //封装一个兼容写法: function getStyle(ele,attr){ //如果浏览器支持该方法,那么返回值是一个函数体;如果不支持是undefined; if(window.getComputedStyle){ return window.getComputedStyle(ele,null)[attr]; }else{ return ele.currentStyle[attr]; } }
相关文章推荐
- js获取元素的外链样式
- js获取元素外链样式的方法
- js获取元素的外链样式的简单实现方法
- 05-JS特效-缓动动画
- js获取元素外链样式的方法
- js获取元素的外链样式
- 原生js获取元素样式
- js获取Dom元素的属性样式
- js获取元素样式方法
- js中获取元素自身样式
- JS利用DOM获取或设置元素的样式
- 如何用js获取元素样式(get.ComputedStyle()和currentStyle方法)
- js获取元素样式方法
- JS一起学05:Date对象、封闭空间、函数传参和封装、获取非行间样式、字符串操作
- JS获取元素样式
- js兼容获取元素的样式
- 用JS获取不到元素样式信息的"Bug"?
- Js 获取行间样式属性值 和 css 内嵌样式或样式表中属性值的方法
- js正确获取元素样式详解
- js获取元素样式