在.js文件中写入图像链接 - Symfony2

Write link to images in .js file - Symfony2

本文关键字:图像 链接 Symfony2 js 文件      更新时间:2023-09-26

我正在使用Symfony2,我的.js文件中有一些指向图像的链接 - 像这样:

$(this).attr('src', 'img/icons/black/icon1.png');

当然,它们不会以这种方式工作:(我应该如何设置它们?

起初我尝试过 {{ asset('bundles/acmemy/img/icons/black/icon1.png') }},但我很快想起我不是在用树枝文件写。

如果您要求使用 JS 文件链接到图像。您可以通过键入来执行此操作IMG SRC="图像/主页.gif" 宽度="100" 高度="30"

如果图像存储在 YourBundle/Resources/Public/img/中然后在 assets:install 之后,它们将可通过此 URL 使用/bundles/yourbundle/img/*

所以答案是: $(this).attr('src', '/bundles/yourbundle/img/icons/black/icon1.png');

您可以在 twig 文件中的 JavaScript 对象中包含资产路径:

<script> var images = {'image1': {{ asset('bundles/acmemy/img/icons/black/icon1.png') }} }; <script>

然后在必须包含在此脚本标记之后的脚本中:

... $(this).attr('src', images.image1); ...