我正在尝试使用coffeescript和jquery设置div的背景图像。我以/images/album_image1.jpg
的形式从DOM中的数据源获取图像url。
但是,当我使用以下命令时:
$('.selector').css({'backgroundImage', "url(#{image_url}"})
CoffeeScript/jQuery生成一个包含主机的URL:
url(http://host.com/images/album_image1.jpg)
此外,Firefox会用引号将URL括起来,而Chrome不会引用URL。
查看此处:JSBin
我想要得到结果url(/images/album_image1.jpg)
,也就是没有主机部分和引号。我希望得到后一种结果,以便通过使用Jasmine的测试。
发布于 2013-03-21 00:19:12
好的,因为你是javascipt,你可能想用'+something+'
输出文本,所以:
var image_url = $('img').data('img');
image_url = str.split(".com"); //split returns an array of everything after the regex: .com
image_url = image_url[1];
$('.selector').css('backgroundImage', "url("+image_url+")");
编辑
So if you want to remove the leading url from the returned string you would use split:
var str = 'http://jsbin.com/images/image_album1.jpg'
str =
image_url = str.split(".com"); //split returns an array of everything after the regex: .com
str[1]; ///images/image_album1.jpg
希望这能有所帮助?但我也更新了上面的内容,所以对你的情况很有帮助。
https://stackoverflow.com/questions/15529028
复制相似问题