简体   繁体   English

使用 jQuery 更新背景图像

[英]Update the background image with jQuery

In jQuery, I'm trying to change the background image of a div:在 jQuery 中,我正在尝试更改 div 的背景图像:

var USR_Settings_Color = $('.social-icons ul li a.active').css('background-image');
// Return : url("https://example.com/fir-DIY.jpg")

After I update the like this:在我这样更新之后:

$('#preview').html('<div style="background-image: '+USR_Settings_Color+'"></div>');

But the result is like this:但结果是这样的:

<div style="background-image:url(" https:="" example.com="" fir-diy.jpg");="" background-clip:="" text;="" -webkit-background-clip:="" color:="" transparent;"=""></div>

I think it's a conflict with the ' and " .我认为这是与'"的冲突。

How I can solve this please?请问我该如何解决?

Thanks.谢谢。

Why do you use Html attr instead of css?为什么使用 Html attr 而不是 css? try do this instead:尝试这样做:

$('#preview').css('background-image', USR_Settings_Color)

by the way I would take all the properties you added in the html attribute, and put them in class, by this you just adding the specific div the class to keep a cleaner code.顺便说一句,我会将您在 html 属性中添加的所有属性放入 class 中,这样您只需添加特定的 div class 以保持代码更清晰。

try use the backtag instead:尝试使用 backtag 代替:

$('#preview').html(`<div style=background-image:${USR_Settings_Color}></div>`);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM