jQuery CSS plugin that returns computed style of element to pseudo clone that element?

后端 未结 9 1167
长情又很酷
长情又很酷 2020-11-22 15:27

I\'m looking for a way using jQuery to return an object of computed styles for the 1st matched element. I could then pass this object to another call of jQuery\'s css method

9条回答
  •  暖寄归人
    2020-11-22 15:55

    Two years late, but I have the solution you're looking for. Here's a plugin I wrote (by wrapping another guy's function in plugin format) which does exactly what you want, but gets all possible styles in all browsers, even IE.

    jquery.getStyleObject.js:

    /*
     * getStyleObject Plugin for jQuery JavaScript Library
     * From: http://upshots.org/?p=112
     *
     * Copyright: Unknown, see source link
     * Plugin version by Dakota Schneider (http://hackthetruth.org)
     */
    
    (function($){
        $.fn.getStyleObject = function(){
            var dom = this.get(0);
            var style;
            var returns = {};
            if(window.getComputedStyle){
                var camelize = function(a,b){
                    return b.toUpperCase();
                }
                style = window.getComputedStyle(dom, null);
                for(var i=0;i

    Basic usage is pretty simple:

    var style = $("#original").getStyleObject(); // copy all computed CSS properties
    $("#original").clone() // clone the object
        .parent() // select it's parent
        .appendTo() // append the cloned object to the parent, after the original
                    // (though this could really be anywhere and ought to be somewhere
                    // else to show that the styles aren't just inherited again
        .css(style); // apply cloned styles
    

    Hope that helps.

提交回复
热议问题