Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将jQuery.css()与变量一起使用_Jquery_Css_Ajax - Fatal编程技术网

将jQuery.css()与变量一起使用

将jQuery.css()与变量一起使用,jquery,css,ajax,Jquery,Css,Ajax,我正在使用jQuery动态更改div上的背景图像。以下是我的代码: $.ajax({ url: '/countries/' + currentHash, type: 'GET', dataType: 'html', complete: function(xhr, textStatus) { //called when complete }, success: function(data, textStatus, xhr) {

我正在使用jQuery动态更改div上的背景图像。以下是我的代码:

$.ajax({
    url: '/countries/' + currentHash,
    type: 'GET',
    dataType: 'html',
    complete: function(xhr, textStatus) {
        //called when complete
    },
    success: function(data, textStatus, xhr) {
        $('#info').empty();

        var country_id = $('div#country_id', data).text();
        $('#fancybox-outer').css('background', "#ffffff url(\"/system/backgrounds/" + country_id + "/large.jpg\") no-repeat top right");
        // $('#fancybox-outer').css('background', '#ffffff url("/system/backgrounds/4/large.jpg") no-repeat top right');

        $.fancybox(
            $('#info').html(),
            {
                autoDimensions   : false,
                width            : '800',
                height           : '600',
                speedIn              : 600,
                speedOut             : 200,
                // scrolling             : 'no',
                padding : 0,
                centerOnScroll : true,
                overlayColor : '#333333',
                overlayOpacity : 0.8,
                transitionIn : 'fade', // 'elastic', 'fade' or 'none'
                transitionOut : 'elastic', // 'elastic', 'fade' or 'none'
                hideOnOverlayClick : false,
            }
        );                                                      

    }
});
代码中的其他一切都很好。我确实拿了一些出来,这样你更容易阅读。它在div中获取country\u id。我已经尝试将country\u id变量附加到页面上的div中,以确保它获取了它并且它是。但这部分根本不起作用:

    var country_id = $('div#country_id', data).text();
    $('#fancybox-outer').css('background', "#ffffff url(\"/system/backgrounds/" + country_id + "/large.jpg\") no-repeat top right");
    // $('#fancybox-outer').css('background', '#ffffff url("/system/backgrounds/4/large.jpg") no-repeat top right');
注释掉的部分工作正常。我该怎么做有什么想法吗


谢谢

尝试如下修改引号:

$('#fancybox-outer').css('background', '#ffffff url("/system/backgrounds/' + country_id + '/large.jpg") no-repeat top right');
您可能还需要确保country_id不包含任何空格

var country_id = $('div#country_id', data).text().trim();

在代码中的该点上,您的country_id变量的值是多少?你确定它包含你所期望的吗?它以什么方式“不起作用”?如果出现javascript错误,url是否未设置?div的url是错误的?您是否有任何错误?是因为它找不到图像吗?我知道url函数有一个问题,它是相对于页面的,而不是css文件。这是修剪!非常感谢!