weixin_33725270 2011-06-21 15:51 采纳率: 0%
浏览 44

结合使用jQuery .css()和变量

I'm using jQuery to dynamically change the background image on a div. Here's my code:

$.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,
            }
        );                                                      

    }
});

Everything else is working great in the code. I actually took some out so that it's easier for your to read. It's getting the country_id within the div. I've tried appending the country_id variable into a div on the page to make sure that it's getting it and it is. But this part just isn't working:

    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');

The commented out part works. Any thoughts on what I should do?

Thanks!

  • 写回答

1条回答 默认 最新

  • weixin_33749131 2011-06-21 15:55
    关注

    Try modifying the quotes like this:

    $('#fancybox-outer').css('background', '#ffffff url("/system/backgrounds/' + country_id + '/large.jpg") no-repeat top right');
    

    You may also need to make sure that country_id doesn't contain any whitespace

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

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?