我正在尝试获取div的背景图像,然后将相同的图像应用于新的div。
JS代码
var bg = $(this).css('background-image');
bg = bg.replace('url(','').replace(')','').replace('http://SERVERIP', '');
console.log(bg);
$('.messages-container').prepend('<div class="message-overview" style="background: url(' + bg + ') no-repeat center center;-webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;">
<img class="overview-picture" src="' + $(this).children('.cloud').children('img').attr('src') + '">
<p class="overview-name">' + tempPartner + '</p>
</div>');
更改的网页
<div class="message-overview" style="background: url(" assets="" profiles="" popatop15="" title_picture.png")="" no-repeat="" center="" center;-webkit-background-size:="" cover;="" -moz-background-size:="" -o-background-size:="" background-size:="" cover;"=""> <img class="overview-picture" src="/assets/profiles/popatop15/profile_picture.png"> <p class="overview-name">popatop15</p>
</div>
不过,这对我来说毫无意义。如您所见,我正在控制台日志记录 bg,并在日志中显示"/assets/profiles/popatop15/title_picture.png"。
为什么在这种情况下 bg 被解释为多个值? :o
我认为这只是一个引号转义问题。您伪造的 css 网址没有用引号括起来。
在您的prepend
中替换以下内容:
url(' + bg + ')
通过这个:
url('' + bg + '')
-----编辑
最后发现,还有一些双引号要从bg
中删除。
您的bg
是从 CSS background-image
属性伪造的,该属性具有这些双引号将地址括在括号之间。
这些双引号会干扰用于包装插入bg
style
字符串的引号。
因此,请使用我在编辑之前的建议,并更改此行:
bg = bg.replace('url(','').replace(')','').replace('http://SERVERIP', '');
对于这个:
bg = bg.replace('url("','').replace('")','').replace('http://SERVERIP', '');