我正在Django中开发应用程序,并且我有这个feed.html模板可以加载不同的部分:

    <div class="container">
       <div class="row">
           {% for piece in pieces %}
                {% include "pieces/pieces_card.html" %}
           {% endfor %}
       </div>
    </div>


在此pieces_card.html中,我想加载执行3D可视化的脚本。我已经完成了下一件事:

<div class="madeleine container" id="id-{{ piece.id }}"></div>


问题是JS脚本失败,它显示此错误MADELEINE[ERR] Target must be a valid DOM Element.
我认为这是因为它是在加载所选ID之前加载的,那么如何才能等到此特定元素准备好之后再加载呢?

JS来了

function showPiece(targ, piece){
    window.onload = function(targ, piece){
        Lily.ready({
                target: targ,  // target div id
                file: 'files',  // file input id
                path: '../../static/js/src', // path to source directory from current html file
        });
    };


    window.onload = function(targ, piece){
            var madeleine = new Madeleine({
                target: targ, // target div id
                data: piece,
                path: '../../static/js/src', // path to source directory from current html file
            });
    };
};


并在pieces_card.html中:

var url_objetivo = '{{ piece.file.url }}'
    console.log("==== " + url_objetivo)
    var id_objetivo = "id-" + {{ piece.id}}
    console.log("==== " + id_objetivo)

    showPiece(targ=id_objetivo,
                        piece=url_objetivo)

最佳答案

更新:

targ删除piecewindow.onload args

window.onload = function(){ ... }


先前的答案:

此JS函数参数语法无效:

showPiece(targ=id_objetivo, piece=url_objetivo)


尝试

showPiece(id_objetivo, url_objetivo)

关于javascript - JavaScript在Django模板中找不到ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59032969/

10-12 17:09