简体   繁体   English

Django,HTML,JS: <img src=“#”> 在HTML中工作,但在JS文件中不工作

[英]Django, HTML, JS: <img src=“#”> work in HTML but in JS file not

If I call <img src="{% static "images/loader.gif" %}" alt="loader.gif"/> in home.html , the gif is displayed (so this should mean that it points correctly to the required folder) but if i try to call it in a js file, the broken icon appears ( screenshot ). 如果我在home.html中调用<img src="{% static "images/loader.gif" %}" alt="loader.gif"/> ,则会显示gif(因此,这意味着它正确指向了必填文件夹), 如果我尝试在js文件中调用它,则会出现损坏的图标( 屏幕截图 )。 Is there a different way to call the images in a JS file (compared to an Html file)? 有没有其他方法来调用JS文件(与HTML文件相比)中的图像?

my situation: 我的情况:

home.html home.html

    ...
    <script src="{% static 'js/conta.js' %}"> </script>

    <a href="{% url 'conta' %}" class="btn btn-primary" role="button" id="btnGo">GO</a><div id="loading"></div>
    ...

conta.js conta.js

    $('#btnGO').click(function(e) {
        e.preventDefault();  

        $(this)
            .removeClass('btn-primary')
            .addClass('btn-danger disabled') 
            .text('WAIT');

        $('#loading').html('<img src="static/images/loader.gif" alt="loader.gif"/> loading...');

                        $.get("/conta/", function() {       
                           alert("FINISH");                    



        $('#btnGo')
            .removeClass('btn-danger disabled')
            .addClass('btn-primary') 
            .text('GO');

        $('#loading').html(''); 


        });
    });
});

Your .js file is not processed by tempate system so template tags are not work here. 您的.js文件未由临时系统处理,因此模板标记在这里不起作用。 Use the hard-coded path to your images: 使用图像的硬编码路径:

$('#loading').html(
         '<img src="/static/images/loader.gif" alt="loader.gif"/> loading...');

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

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