0

I let my users to change any attribute(location, color, and ...) of every element in a page before printing.

I'm looking for a way which user can store the new style sheet and restore it anytime later?

I've tried to parse element.style, but it doesn't return an array! Also $(element).css() doesn't work without any argument!

3 Answers3

1

If you are okay with using a plugin for your task, try this plugin : https://github.com/reworkcss/css-parse

Demo:http://bililite.com/blog/blogfiles/cssparser/cssparsertest.php

Example: CSS:

body {background: #eee;color: #888;}

Output:

{
    "type": "stylesheet",
        "stylesheet": {
        "rules": [{
            "type": "rule",
                "selectors": [
                "body"],
                "declarations": [{
                "type": "declaration",
                    "property": "background",
                    "value": "#eee"
            }, {
                "type": "declaration",
                    "property": "color",
                    "value": "#888"
            }]
        }]
    }
}
K K
  • 17,794
  • 4
  • 30
  • 39
1

Thanks to Johan I found the correct answer! I only need to parse

window.getComputedStyle(element);

tnx everyone:)

0

Instead of assigning custom styles to every element you could use unique style classes and a single stylesheet for the entire page (add it to the head, remembering the element or giving it a unique id) which contains your custom styles.

You can then enumerate the stylesheet through stylesheet.cssRules and build your custom json

Jens Elstner
  • 128
  • 1
  • 10