使用jquery保存元素的所有css属性 [英] Save all css properties of element using jquery

查看:139
本文介绍了使用jquery保存元素的所有css属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上试图保存一个元素在局部var /数组中的所有当前css属性。
我试过:

I'm basically trying to save all current css properties of an element in a local var/array. I tried :

el.css();

el.css("*");

没有运气。

推荐答案


  • demo: http://so.devilmaycode.it/save-all-css-properties-of-element-using-jquery

我更新了答案更有效,还提供了一个工作演示...

i have updated the answer to be more efficent also providing a working demo...

    $(function() {
        // element tag example p and element id
        function get_element_style(element, id){
            var css = {};
            $('<iframe id="get-style-'+id+'" style="display:none"/>').appendTo('body');
            $('#get-style-'+id).contents().find('body').append('</'+element+'>');
            $el = $('#get-style-'+id).contents().find('body').find(element);
            var defaults_css = $el.getStyles();
            $('#get-style-'+id).remove();
            var element_css = $('#'+id).getStyles();
            for (var i in element_css) {
                if (element_css[i] !== defaults_css[i]) {
                    css[i] = element_css[i];
                }
            } 
            return css;
        }

        var properties = get_element_style('p', 'test-p');

    });

获取元素的样式时出现的问题是,您不能获得刚刚设置的值,默认值。使用这个代码我试图获取一个元素的设置值。这个工作与 inline 风格以及< style> < link> ;

the problem when getting the style of an element is that you don't get just setted value but also the default values. with this code i'm trying to get just the setted values of an element. this work both with inline style as well as with <style> and <link>


  • NOTE: this solution require the use of this plugin https://github.com/moagrius/copycss

这篇关于使用jquery保存元素的所有css属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆