javascript 获取特定的 CSS属性值
<link id="system_style" type="text/css" href="global.css" rel="stylesheet"/>
<div id="myArticle">
...
</div>
在global.css中声明了
#myArticle{
width:400px;
height:300px;
}
这样的情况下,直接通过JS进行getElementById('myArticle').style.width是无法获取400px的值的,因为这个数值定义在CSS里,所以,必须要用其他方法,我写了以下函数:
/**
* 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<rss.length;i )
{
style = rss[i];
if(style.selectorText.toLowerCase() == labname.toLowerCase())
{
value = style.style[param];
}
}
return value;
}
现在只要通过
getStyleValue(0,'#myArticle','width')
就可以获得啦:)
<div id="myArticle">
...
</div>
在global.css中声明了
#myArticle{
width:400px;
height:300px;
}
这样的情况下,直接通过JS进行getElementById('myArticle').style.width是无法获取400px的值的,因为这个数值定义在CSS里,所以,必须要用其他方法,我写了以下函数:
/**
* 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<rss.length;i )
{
style = rss[i];
if(style.selectorText.toLowerCase() == labname.toLowerCase())
{
value = style.style[param];
}
}
return value;
}
现在只要通过
getStyleValue(0,'#myArticle','width')
就可以获得啦:)
相关推荐
qiupu 2020-11-04
多读书读好书 2020-11-03
RedCode 2020-10-28
jiedinghui 2020-10-25
Ladyseven 2020-10-22
hellowzm 2020-10-12
zuncle 2020-09-28
Ladyseven 2020-09-11
jiedinghui 2020-09-07
xiaohuli 2020-09-02
葉無聞 2020-09-01
impress 2020-08-26
ThikHome 2020-08-24
nicepainkiller 2020-08-20
hellowzm 2020-08-18