在jquery字符串中包含background-image url css [英] Include background-image url css in jquery string

查看:79
本文介绍了在jquery字符串中包含background-image url css的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在jquery字符串中包含css样式background-image:url('')。此字符串在网址处中断('

How do I include the css style background-image:url('') in a jquery string. This string breaks at the url('

 var img=field.img;
 $(".wrapper").append("<div class='slide bgimage' style='background-image: url('img/" + img + ".jpg');'></div>");


推荐答案

字符串断开,因为[style =]后的开头单引号结束在[url(]。
由于字符串已经包含单引号和双引号,你必须逃避第3个。

The string breaks because the opening single quotation mark after [style=] ends at [url(]. Since the string already contains both single quotation marks aswell as double quotation marks, you have to escape the 3rd.

更改

您应该更改代码

$(".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>

这篇关于在jquery字符串中包含background-image url css的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆