简体   繁体   English

如何将html文件的内容加载到Django的javascript变量中?

[英]How to load a html file's content into javascript variable in django?

I'd like to insert contents of abc.html after a div when some event fires. 当某些事件触发时,我想在div之后插入abc.html的内容。

I tried 我试过了

var content = {% include "path/to/abc.html" %};

$(".myDiv").click(function() {

$("#anotherDiv").insertAfter(content);
});

However, it seems `{% include %} fails because of some sort of parsing error. 但是,由于某些解析错误,看来{%include%}失败了。 (abc.html is multi-line) (abc.html是多行)

abc.html contains django filters/tags. abc.html包含Django过滤器/标签。

any thoughts? 有什么想法吗?

If abc.html is pure html, then you could do a simple ajax request to load the file, eg 如果abc.html是纯html,则可以执行一个简单的ajax请求来加载文件,例如

$(".myDiv").click(function() {
    $.get('path/to/abc.html', {}, function(content) {
        $(content).insertAfter("#anotherDiv");
    });
});

Although if you're worried about loadtime, you could load it earlier into a variable. 尽管如果您担心加载时间,则可以更早地将其加载到变量中。

Let me know if the code makes sense or if you have any questions :) 让我知道代码是否有意义或您有任何问题:)

一种替代方法是将内容放入一个隐藏的div中,然后使您的click函数取消隐藏它。

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

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