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

WBOY
Release: 2016-05-16 12:05:02
Original
1615 people have browsed it


...
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 :)
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!