[英]JQuery to reload DIV? (Flask/Jinja2)
我搜索無濟於事。我想我的問題有點獨特。 我有一個DIV,其中包含一些內容,當前是通過將SQL查詢的對象傳遞到模板中來引入該內容的。 效果很好,但不允許我每x秒更新一次內容,而無需刷新整個頁面。 我不想做的事。 所以我想知道我可以強迫JQuery重新加載DIV元素嗎? 這還會重新加載其中的代碼嗎? 因為那樣會迫使我的div更新。這是代碼。
<div id=content1>
{{ set content = get_content(parm1, parm2) }} # This is a custom Jinja2 function I wrote to pull the content I need one in the template, but it only runs once.
{% for row in content %}
.. do stuff
{% endfor %}
</div>
是否有可能讓JQuery只是用已經存在的代碼重新加載該DIV? 如果是這樣,那應該可以解決我的問題。 我曾考慮過嘗試創建一個調用以獲取JSON中的內容,但如果可能的話,寧願不這樣做。
app.py
@app.route('/')
def some_view():
name = request.args.get('name', 'Anonymous')
if request.is_xhr:
template_name = 'template_ajax.html'
else:
template_name = 'template.html'
return render_template(template_name, name=name)
template.html
<html>
<head>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<head>
<body>
<h3>Title</h3>
<div>Hello, {{ name }}!</div>
<p>Some text</p>
<script>
$(document).ready(function() {
$('div').load('/?name=username');
});
</script>
</body>
</html>
template_ajax.html
Hello from AJAX, {{ name }}!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.