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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-05-16 12:05:02
Original
1736 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:
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
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template