jQuery - How to get all styles/css (defined within internal/external document) with HTML of an element

前端 未结 9 2013
不知归路
不知归路 2020-12-01 08:22

I know that $(\"#divId\").html() will give me innerHtml. I also need its styles (which might be defined by the means of classes) either in-line style

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 08:58

    if you want to save all of the style of an element i think this will be more complicated as you think first of all my first ide was the firebug css console. this shows all fo the style of an element and i thought how? so i searched for the source code of the firebug and i found this:

    http://fbug.googlecode.com/svn/branches/firebug1.7/content/firebug/css.js

    this code working only on the css part.

    const styleGroups =
    {
        text: [
            "font-family",
            "font-size",
            "font-weight",
            "font-style",
            "color",
            "text-transform",
            "text-decoration",
            "letter-spacing",
            "word-spacing",
            "line-height",
            "text-align",
            "vertical-align",
            "direction",
            "column-count",
            "column-gap",
            "column-width"
        ],
    
        background: [
            "background-color",
            "background-image",
            "background-repeat",
            "background-position",
            "background-attachment",
            "opacity"
        ],
    
        box: [
            "width",
            "height",
            "top",
            "right",
            "bottom",
            "left",
            "margin-top",
            "margin-right",
            "margin-bottom",
            "margin-left",
            "padding-top",
            "padding-right",
            "padding-bottom",
            "padding-left",
            "border-top-width",
            "border-right-width",
            "border-bottom-width",
            "border-left-width",
            "border-top-color",
            "border-right-color",
            "border-bottom-color",
            "border-left-color",
            "border-top-style",
            "border-right-style",
            "border-bottom-style",
            "border-left-style",
            "-moz-border-top-radius",
            "-moz-border-right-radius",
            "-moz-border-bottom-radius",
            "-moz-border-left-radius",
            "outline-top-width",
            "outline-right-width",
            "outline-bottom-width",
            "outline-left-width",
            "outline-top-color",
            "outline-right-color",
            "outline-bottom-color",
            "outline-left-color",
            "outline-top-style",
            "outline-right-style",
            "outline-bottom-style",
            "outline-left-style"
        ],
    
        layout: [
            "position",
            "display",
            "visibility",
            "z-index",
            "overflow-x",  // http://www.w3.org/TR/2002/WD-css3-box-20021024/#overflow
            "overflow-y",
            "overflow-clip",
            "white-space",
            "clip",
            "float",
            "clear",
            "-moz-box-sizing"
        ],
    
        other: [
            "cursor",
            "list-style-image",
            "list-style-position",
            "list-style-type",
            "marker-offset",
            "user-focus",
            "user-select",
            "user-modify",
            "user-input"
        ]
    };
    

    the function which gets all of the styles.

    updateComputedView: function(element)
    {
        var win = element.ownerDocument.defaultView;
        var style = win.getComputedStyle(element, "");
    
        var groups = [];
    
        for (var groupName in styleGroups)
        {
            var title = $STR("StyleGroup-" + groupName);
            var group = {title: title, props: []};
            groups.push(group);
    
            var props = styleGroups[groupName];
            for (var i = 0; i < props.length; ++i)
            {
                var propName = props[i];
                var propValue = stripUnits(rgbToHex(style.getPropertyValue(propName)));
                if (propValue)
                    group.props.push({name: propName, value: propValue});
            }
        }
    
        var result = this.template.computedTag.replace({groups: groups}, this.panelNode);
        dispatch(this.fbListeners, 'onCSSRulesAdded', [this, result]);
    }
    
    function stripUnits(value)
    {
        // remove units from '0px', '0em' etc. leave non-zero units in-tact.
        return value.replace(/(url\(.*?\)|[^0]\S*\s*)|0(%|em|ex|px|in|cm|mm|pt|pc)(\s|$)/gi, function(_, skip, remove, whitespace) {
        return skip || ('0' + whitespace);
        });
    }
    

    in this code i figured out that the

    win.getComputedStyle(element, "")
    

    to get all of the styles of an element, and then with a for loop gets all of the style and prints out. so i think the getComputedSTyle is the main function to use, and after this you can get the props one by one with:

    style.getPropertyValue(propName)
    

提交回复
热议问题