用jquery选择div值的背景图像

时间:2011-06-01 15:54:16

标签: jquery jquery-selectors

我正在尝试获取某些div的背景图像的css值,但我不知道如何。

我知道我可以使用方法属性

获得所有样式
$(document).ready(initialize);

var imageAux;

function initialize() {

 $(".imagePanel").click(function () {

    var aux = $(this);

    if (imageSelected != aux) {

        if (imageSelected != null) {

            imageSelected.stop(true, true).data('selected', false);
            imageSelected.css('border', '0 none');
            imageSelected.css('opacity', '1');
        }

        aux.css('border', '2px solid black').data('selected', true);
        imageSelected = aux;

        //pass the value to me aux variable
        imageAux = imageSelected;
    }

});

function manageAccordion() {

    var imagePath = imageAux.attr("style");
}

我尝试了类似的东西,但它不起作用

imageAux.attr("style").find('background-image')

imageAux.attr("style background image"

我需要一个选择器给我一个我已经保存在变量imageAux中的div的背景图像

2 个答案:

答案 0 :(得分:4)

使用jQuery.css功能

imageAux.css('background-image');

答案 1 :(得分:0)

使用.attr()时,您在请求的属性中可能有多个值。例如,如果你有以下div,你将有多个类名(在这种情况下,'class1 class2'):

<div class="class1 class2"></div>

如果您要查找特定的CSS值,请使用jQuery.css method