我搜索无果。我想我的问题有点独特。我有一个包含一些内容的 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 }}!
关于javascript - JQuery重新加载DIV? ( flask /Jinja2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42746801/