简体   繁体   English

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

[英]Write link to images in .js file - Symfony2

I'm using Symfony2 and I have some links to images in my .js file - like this: 我正在使用Symfony2,并且我的.js文件中有一些图像链接,如下所示:

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

Of course they don't work this way :( How should I set them? 当然,它们不是这样工作的:(我应该如何设置它们?

At first I tried with {{ asset('bundles/acmemy/img/icons/black/icon1.png') }} , but I soon remembered that I'm not writing in twig file. 最初,我尝试使用{{ asset('bundles/acmemy/img/icons/black/icon1.png') }} ,但是很快我就想起我不是在树枝文件中编写。

If you are asking to link to image using JS file. 如果您要使用JS文件链接到图像。 You can do that by typing IMG SRC="images/home.gif" WIDTH="100" HEIGHT="30" 您可以通过输入IMG SRC =“ images / home.gif” WIDTH =“ 100” HEIGHT =“ 30”来完成此操作

if images stored in YourBundle/Resources/Public/img/ then after assets:install they will be available by this url /bundles/yourbundle/img/* 如果图像存储在YourBundle / Resources / Public / img /中,则在资产:安装后,可通过此URL获得它们/ bundles / yourbundle / img / *

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

you may include you assets paths in a javascript object inside your twig file: 您可以将资产路径包括在树枝文件中的javascript对象中:

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

and then in your script that has to be included after this script tag: 然后在脚本中必须包含在此脚本标记之后的脚本中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM