当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JS获得CSS中声明的特定样式中的某属性值

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 JS获得CSS中声明的特定样式中的某属性值


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-13   浏览: 437 ::
收藏到网摘: n/a

用途:用于获得在CSS文件中声明的特定样式中的某属性值。如:

<link id="system_style" type="text/css" href="global.css" rel="stylesheet"/>
<div id="myArticle" style="left:10px;top:35px;" >

在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')

就可以获得啦:)