js获取当前有效样式】的更多相关文章

JS获取内联样式 //获取内联样式 function getCss(obj,attr){//obj:对象,name:style属性 if(obj.currentStyle) { return obj.currentStyle[attr]; } else { return document.defaultView.getComputedStyle(obj,null)[attr]; } } 大家都知道,用document.getElementById(‘element').style.xxx可以获取…
我们都知道用offset函数获取元素样式是一件很方便的事,但是offset只能获取行间样式,而无法获得非行间样式,这是它的瓶颈所在. 我们都知道js获取行间样式的方法,那么js是如何获取行距样式的呢? 首先让我们看一下js是如何获取行间样式的: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona…
通过js获取外部样式表中的属性,比如.box在style.css样式表中有个属性叫font-size:16px; 通过js获取.box的这个属性: js代码为: <script> window.onload=function(){ var oBox  = document.getElementsByClassName("box")[0]; alert(oBox.style.fontSize);//结果返回的是空 } </script> 解决方法: js代码为:…
  js获取有效样式   节点.currentStyle["属性名"]        兼容ie方法(只有ie有效) getcomputedStyle(节点)["属性名"]                兼容火狐.谷歌(谷歌火狐有效)   总结:既封装的函数为       function getStyle(node, styleType){         return node.currentStyle? node.currentStyle[styleType]:…
工作中经常会需要获取DOM元素的样式,之前都是通过jquery的css()方法,现在总结一下通过原生js获取元素样式的方法. obj.style js var _width = obj.style.width; 该方法基本可以忽略,因为他只能获取到内敛样式.而现在很少会写内联样式.当然他有个好处是可读可写,所以可以据此设置元素的样式. window.getComputedStyle js // 语法如下: window.getComputedStyle('元素','伪元素'); var _css…
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-…
<!--DOCTYPE html--> <html> <head> <meta charset="utf-8" /> <style> *{ text-align:center;} input{ margin-top:30px; padding:10px 20px;} #div1{ width:500px; height:300px; background:red; margin:10px auto;} </style&g…
获取非行间样式: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>获取非行间样式</title> <style> #div1{height: 200px;width: 200px;background-color: red;} </style> <script> fun…
<!--DOCTYPE html--> <html> <head> <meta charset="utf-8" /> <style> *{ text-align:center;} input{ margin-top:30px; padding:10px 20px;} #div1{ width:500px; height:300px; background:red; margin:10px auto;} </style&g…
<!--DOCTYPE html--> <html> <head> <meta charset="utf-8" /> <style> *{ text-align:center;} input{ margin-top:30px; padding:10px 20px;} #div1{ width:500px; height:300px; background:red; margin:10px auto;} </style&g…