Can jQuery get all CSS styles associated with an element?

前端 未结 5 1153
谎友^
谎友^ 2020-11-21 06:31

Is there a way in jQuery to get all CSS from an existing element and apply it to another without listing them all?

I know it would work if they were a style attribut

5条回答
  •  清酒与你
    2020-11-21 07:02

    @marknadal's solution wasn't grabbing hyphenated properties for me (e.g. max-width), but changing the first for loop in css2json() made it work, and I suspect performs fewer iterations:

    for (var i = 0; i < css.length; i += 1) {
        s[css[i]] = css.getPropertyValue(css[i]);
    }
    

    Loops via length rather than in, retrieves via getPropertyValue() rather than toLowerCase().

提交回复
热议问题