javascript 获取元素的真实,最终的css样式
2012-09-20 16:46
751 查看
elem.style.xxx只能获取内联的css属性,对<head>内的或link外部样式的无能为力。
其实
w3c办法,它使用的是通用的text-align而非textAlign;所以先要把属性名替换。
注意;几乎任何属性都会返回表示元素样式的字符串而非数值(比如是100px而非100);
上面展示了如何获取一个dom元素的css属性的最终真实值。需要注意的是:这个函数忽略的其他的计量单位(如百分比)。所以这个方法并不完美,但已经是一个很好的开端。
chrome下height:x%;
不过是x是什么,都是18。
Ie的currentStyle对象代表了在全局样式表、内嵌样式和HTML标签属性中指定的对象格式和样式。http://msdn.microsoft.com/en-us/library/ie/ms535231(v=vs.85).aspx
[/code]
usedvaluesofalltheCSSpropertiesofanelement.[/code]
pseudoEltOptional
Astringspecifyingthepseudo-elementtomatch.Mustbe
通常的元素必须为null。
[/code]
<style> h1{ font-size:1.6em; color:red; } </style> </head> <body> <h1id="title">helloworld</h1> <p>thisisatesttxt</p> <script> varx=document.getElementById("title"); document.write(x.style.color); </script>
其实
x.style.color=""; 所以看不到样式信息. AfunctionforfindingthecomputedstylevalueofanelementisshowninListing7-1
//Getastyleproperty(name)ofaspecificelement(elem) functiongetStyle(elem,name){ //Ifthepropertyexistsinstyle[],thenit'sbeenset //recently(andiscurrent) if(elem.style[name]) returnelem.style[name]; //Otherwise,trytouseIE'smethod elseif(elem.currentStyle) returnelem.currentStyle[name]; //OrtheW3C'smethod,ifitexists elseif(document.defaultView&&document.defaultView.getComputedStyle){ //Itusesthetraditional'text-align'styleofrulewriting, //insteadoftextAlign name=name.replace(/([A-Z])/g,"-$1"); name=name.toLowerCase(); //Getthestyleobjectandgetthevalueoftheproperty(ifitexists) vars=document.defaultView.getComputedStyle(elem,""); returns&&s.getPropertyValue(name); //Otherwise,we'reusingsomeotherbrowser }else returnnull; }
w3c办法,它使用的是通用的text-align而非textAlign;所以先要把属性名替换。
注意;几乎任何属性都会返回表示元素样式的字符串而非数值(比如是100px而非100);
<html> <head> <style>p{height:100px;}</style> <script> window.onload=function(){ //Locatetheparagraphtochecktheheightof varp=document.getElementsByTagName(“p”)[0]; //Checktheheightthetraditionalway alert(p.style.height+“shouldbenull”); //Checkthecomputedvalueoftheheight alert(getStyle(p,“height”)+“shouldbe100px”); }; </script> </head> <body> <p>Ishouldbe100pixelstall.</p> </body> </html>
上面展示了如何获取一个dom元素的css属性的最终真实值。需要注意的是:这个函数忽略的其他的计量单位(如百分比)。所以这个方法并不完美,但已经是一个很好的开端。
chrome下height:x%;
不过是x是什么,都是18。
Ie的currentStyle对象代表了在全局样式表、内嵌样式和HTML标签属性中指定的对象格式和样式。
W3c: document.defaultView browsersreturnstheifnoneavailablewindowobjectassociatedwiththedocumentor[code]null
[/code]
varwin=document.defaultView; [code]getComputedStyle()givesthefinal
varstyle=window.getComputedStyle(element[,pseudoElt]); Thereturned[code]styleisa
CSSStyleDeclarationobject.
pseudoEltOptional
Astringspecifyingthepseudo-elementtomatch.Mustbe
null(ornotspecified)forregularelements.
通常的元素必须为null。
[/code]
方法[/code][code]CSSStyleDeclaration
getPropertyValue(propertyName) removeProperty(propertyName) 参考:https://developer.mozilla.org/en-US/docs/DOM/window.getComputedStyle
相关文章推荐
- Javascript操作CSS之3——获取元素的真实高度和宽度
- 如何获取html中元素的最终的css属…
- JavaScript 获取元素的css属性
- javascript 获取元素样式的方法
- javascript 获取最终样式的函数
- 原生javascript获取元素样式
- JavaScript改变HTML元素的样式改变CSS及元素属性
- JavaScript_设置和获取元素样式
- javascript获取作用在元素上面的样式属性代码
- JavaScript获取元素CSS属性
- javascript 获取元素样式必杀技
- javascript获取元素样式
- JavaScript获取元素样式
- JavaScript基础插曲—获取标签,插入元素,操作样式
- 获取元素的真实,最终的css样式属性值的函数
- JavaScript基础插曲—获取标签,插入元素,操作样式
- Javascript 如何获取 Css 中的样式
- CSS getStyle 获取元素的样式,解决offSet+样式所产生的bug
- JavaScript-给iframe里的内容定义样式 获取元素
- JavaScript改变HTML元素的样式改变CSS及元素属性