如何在 jquery 字符串中包含 css 样式 background-image:url('') 。此字符串在 url('
var img=field.img;
$(".wrapper").append("<div class='slide bgimage' style='background-image: url('img/" + img + ".jpg');'></div>");
最佳答案
字符串中断是因为 [style=] 之后的左单引号在 [url(] 处结束。 由于字符串已经同时包含单引号和双引号,因此您必须转义第 3 个。
改变
你应该改变代码
$(".wrapper").append("<div class='slide bgimage' style='background-image: url('img/" + img + ".jpg');'></div>");
到
$(".wrapper").append("<div class='slide bgimage' style='background-image: url(\"img/" + img + ".jpg\");'></div>");
示例
var field = {
img: "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fa/140626_Tierser_Alpl_Rossz%C3%A4hne.jpg/245px-140626_Tierser_Alpl_Rossz%C3%A4hne"
}
var img=field.img;
$(".wrapper").append("<div class='slide bgimage' style='background-image: url(\"" + img + ".jpg\");'></div>");
.slide {display:inline-block;width:400px;height:400px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="wrapper"></div>
关于javascript - 在 jquery 字符串中包含背景图像 url css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40208825/