我正在使用此代码上传背景图像并将其附加到类:
// part of ajax
success: function(data) {
let url = 'https://example.digitaloceanspaces.com/' + data.url;
$('.product_header').css({"background-image":"url('" + url + "')"});
//saving style attribute to global variable
styles = $('.product_header').attr('style');
},
字符串已保存为:'background: url("https://example.digitaloceanspaces.com/L0zk2.png");'
在那之后,我需要使用样式字符串来附加,我正在尝试实现这样的字符串:
$('#product_header').append('<div class="product_header" data-slide-to="11" style="' + style + '"></div>');
但当我检查浏览器控制台时,它的渲染像:
<div class="product_header" style="background: url(" https:="" pdfdocs.nyc3.digitaloceanspaces.com="" l0zk2.png");"=""></div>
并且它不能正确地显示背景图像。
我该如何解决这个问题?我在想。在jquery中append从URL中删除"斜杠",但为什么?
这行中可能有一组额外的引号:
$('.product_header').css({"background-image":"url('" + url + "')"});
尝试将其更改为:
$('.product_header').css({"background-image":"url(" + url + ")"});
https://www.tutorialrepublic.com/faq/how-to-set-css-background-image-property-using-jquery.php