开发者

How to read from CSS files with jQuery

开发者 https://www.devze.com 2023-01-02 07:08 出处:网络
Ok, I have a HTML page with jQuery included. I have a CSS file with a good load of lines in, I would like to read all styles for a given element from the externa开发者_如何学Cl CSS file... Not the in

Ok, I have a HTML page with jQuery included.

I have a CSS file with a good load of lines in, I would like to read all styles for a given element from the externa开发者_如何学Cl CSS file... Not the inline styles...

I have the following code (which looks like it should work...):

var styleProperties= {};
var getCssProperties = ['width', 'margin', 'height']; 

for (c=0;c<=returnStyleProps.length;c++) {
    styleProperties[returnStyleProps[c]] = $('div#container').css(returnStyleProps[c]);
    alert(styleProperties);
}
alert(styleProperties);

But this only seems to alert:

"[Object Object]"

[edit] Please...? I'm really quite stuck, nothing seems to work :( [/edit]


var cs = {};
var elem = $('h1')[0];
for(var s in elem.style)
{
    // console.log(s + typeof(s));
    var v = $(elem).css(s);
    if (v && v != '')
    {
        cs[s] = v;
    }
}

for(var s in cs)
{
    console.log(s + ': ' + cs[s]);
}

please run in firebug if you have h1 element on the page and jQuery is used... just a rough idea though...


to start, please try console.log(styleProperties); and look at what the object holds in firebug console...


Using firebug put a breakpoint on the code and you will be able to inspect the properties of said object. That should let you know what it is and if it is what you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消