JavaScript 在 Django 模板中找不到 id



我正在 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)

更新:

window.onload中删除targpiece参数

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

以前的答案:

此 JS 函数参数语法无效:

showPiece(targ=id_objetivo, piece=url_objetivo)

尝试

showPiece(id_objetivo, url_objetivo)

相关内容

  • 没有找到相关文章

最新更新