我正在尝试获取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>');
更改了html
<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会解释为多个值? :○
答案 0 :(得分:1)
我认为这只是一个引用逃避问题。 你的伪造的css网址没有用引号括起来。
在prepend
替换此内容:
url(' + bg + ')
由此:
url(\'' + bg + '\')
的 -----
修改
最后证明,还有一些双引号要从bg
中删除
您的bg
是由CSS background-image
属性伪造的,该属性在括号之间包含地址的双引号。
这些双引号会干扰用style
字符串插入bg
的地方。
所以,使用我在此修改之前建议的内容以及更改此行:
bg = bg.replace('url(','').replace(')','').replace('http://SERVERIP', '');
这个:
bg = bg.replace('url("','').replace('")','').replace('http://SERVERIP', '');