如何解析引用 html、javascript 中背景图像 url 中的字符串?

How to parse a string in background-image url in quoted html,javascript?

在从控制器到 index.html.twig 的 ajax 调用(图像名称:Logo,JGP - lille70720150924032031.jpg)中获取图像名称作为响应后在 symfony2 中,将图像放入 javascript table 变量,如下所示。

data[index].Interestlogo=图片名称;

    table="";
    table += "<div class='imgparam th' style='background-image: url({{basepath}}/uploads/Interests/"+data[index].Idinterestmaster+"/thumbnails/"+data[index].Interestlogo+")'></div>";
    $("#results").append(table);

这里所有正常的图像名称都工作正常。 if image name=Logo, JGP - lille70720150924032031.jpg then image is not displaying.

这里我需要在下面加上双引号 背景图片:url("路径应该放在这里");

我得到的结果如下 background-image: url(path should sit here);//没有双引号

请教我,双引号怎么写。

如果您要动态创建 URI,则必须确保它们是网络安全 URI。

Is a URL allowed to contain a space?

它们不能包含空格。但这就是为什么当您查看自己的问题的标题时,它有很多破折号,

/questions/32783366/how-to-parse-a-string-in-background-image-url-in-quoted-html-javascript

我建议 运行 你的代码通过 URL 编码器。

Encode URL in JavaScript?

希望对您有所帮助

经过大量研究,最后我很高兴我得到了 Solution.I 已经分开的 url 路径并放入了 var url 路径。 对于 url路径应用引号,如答案代码中一样使用斜杠。

最后,带引号的 url 已替换为 background_image url。

var urlpath="{{basepath}}/uploads/Interests/"+data[index].Idinterestmaster+"/thumbnails/"+data[index].Interestlogo;
var withdoublequotes="\""+urlpath+"\"";
table += "<div class='imgparam th' style='background-image: url("+withdoublequotes+")'></div>";