Home  >  Article  >  Web Front-end  >  javascript gets specific CSS attribute values ​​​​Experience exchange

javascript gets specific CSS attribute values ​​​​Experience exchange

WBOY
WBOYOriginal
2016-05-16 12:05:021615browse


...
div>

Declared in global.css


#myArticle{
width:400px;
height:300px;
}

In this case, it is impossible to obtain the value of 400px through getElementById('myArticle').style.width directly through JS, because this value is defined in CSS, so other methods must be used. I wrote the following function:


/**
* function for get the style value in special css file
* @param int css_file_id
* @param String labname
* @param String param
*/
function getStyleValue(css_file_id,labname,param)
{
var tar;
var rss;
var style;
var value;

tar = document.styleSheets[css_file_id];

rss = tar.cssRules?tar.cssRules:tar.rules

for(i=0;i{
style = rss[i];
if(style.selectorText.toLowerCase() == labname.toLowerCase())
{
value = style.style[param]; }
}
return value;
}

Now just pass


getStyleValue(0,'#myArticle','width')

You can get it :)
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn