使用jQuery获取多个CSS属性 [英] Get multiple CSS properties with jQuery

查看:110
本文介绍了使用jQuery获取多个CSS属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以像这样设置多个css属性:

I know you can SET multiple css properties like so:

$('#element').css({property: value, property: value});

但我如何用CSS获取多个属性?
是否有任何解决方案?

But how do I GET multiple properties with CSS? Is there any solution at all?

推荐答案

您可以创建自己的jQuery函数来执行此操作:

You can create your own jQuery function to do this: ​

//create a jQuery function named `cssGet`
$.fn.cssGet = function (propertyArray) {

    //create an output variable and limit this function to finding info for only the first element passed into the function
    var output = {},
        self   = this.eq(0);

    //iterate through the properties passed into the function and add them to the output variable
    for (var i = 0, len = propertyArray.length; i < len; i++) {
        output[propertyArray[i]] = this.css(propertyArray[i]);
    }
    return output;
};

这是一个演示: http://jsfiddle.net/6qfQx/1/ (检查控制台日志以查看输出)

Here is a demo: http://jsfiddle.net/6qfQx/1/ (check your console log to see the output)

这function需要传入一个数组,包含要查找的CSS属性。用法如下:

This function requires an array to be passed in containing the CSS properties to look-up. Usage for this would be something like:

var elementProperties = $('#my-element').cssGet(['color', 'paddingTop', 'paddingLeft']);
console.log(elementProperties.color);//this will output the `color` CSS property for the selected element

这篇关于使用jQuery获取多个CSS属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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