简体   繁体   中英

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.

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. (abc.html is multi-line)

abc.html contains django filters/tags.

any thoughts?

If abc.html is pure html, then you could do a simple ajax request to load the file, eg

$(".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函数取消隐藏它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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