2

我正在尝试在静态 js 脚本中设置图像,但它不起作用。

var _pointer = document.createElement("DIV");
_pointer.style.backgroundImage = 'url(../images/repeater_1x4.jpg)';

它返回此错误:

GET http://localhost:9000/images/repeater_1x4.jpg 404 (Not Found)

我还尝试了以下操作,因为我将静态资源从公用文件夹映射到 assets/url 路径,它不会返回任何错误,但又不会显示图像。

var _pointer = document.createElement("DIV");
_pointer.style.backgroundImage = 'url(@routes.Assets.at("images/repeater_1x4.jpg"))';

如果您想知道这是在我的路线文件中:

GET     /assets/*file               controllers.Assets.at(path="/public", file)

这样做的正确方法是什么?我正在使用播放!2.0,我的脚本在 public/javascripts 中,我的图像在 public/images 中,正如您所料。

4

1 回答 1

5

利用:

_pointer.style.backgroundImage = 'url(/assets/images/repeater_1x4.jpg)';
于 2012-11-19T19:29:48.863 回答