Setting background-image using jQuery CSS property

前端 未结 11 2126
渐次进展
渐次进展 2020-11-22 10:34

I have an image URL in a imageUrl variable and I am trying to set it as CSS style, using jQuery:

$(\'myObject\').css(\'background-image\', image         


        
11条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 10:56

    You'll want to include double quotes (") before and after the imageUrl like this:

    $('myOjbect').css('background-image', 'url("' + imageUrl + '")');
    

    This way, if the image has spaces it will still be set as a property.

提交回复
热议问题