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

后端 未结 9 1084
长情又很酷
长情又很酷 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:43

    Great function provided by the OP. I slightly modified it so that you can choose which values you want returned.

    (function ($) {
        var jQuery_css = $.fn.css,
            gAttr = ['font-family','font-size','font-weight','font-style','color','text-transform','text-decoration','letter-spacing','word-spacing','line-height','text-align','vertical-align','direction','background-color','background-image','background-repeat','background-position','background-attachment','opacity','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','position','display','visibility','z-index','overflow-x','overflow-y','white-space','clip','float','clear','cursor','list-style-image','list-style-position','list-style-type','marker-offset'];
        $.fn.css = function() {
            if (arguments.length && !$.isArray(arguments[0])) return jQuery_css.apply(this, arguments);
            var attr = arguments[0] || gAttr,
                len = attr.length,
                obj = {};
            for (var i = 0; i < len; i++) obj[attr[i]] = jQuery_css.call(this, attr[i]);
            return obj;
        }
    })(jQuery);
    

    Choose which values you want by specifying your own array: $().css(['width','height']);

提交回复
热议问题