javascript - JQuery重新加载DIV? ( flask /Jinja2)

标签 javascript jquery python 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 }}!

关于javascript - JQuery重新加载DIV? ( flask /Jinja2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42746801/

相关文章:

jquery - 打字时显示使用 jquery 的东西?

python - pygame,如何在调整窗口大小时更新窗口大小

python - 找到给定数字上方和下方最接近的元素

python - 根据引用重新排序多索引数据框列

javascript - 为什么在使用 JQuery().html() 时 .innerHTML 问题不是问题?

php - 执行查询加载页面

javascript - 名为 'click' 的 Jquery Ajax 在未实现接口(interface) HTMLElement 的对象上调用

jquery - 在 jQuery 中创建一个更改图像的间隔?

javascript - Angularjs 从列表创建 div 网格

javascript - 如何从服务响应中停止 html 上的多个事件处理程序 onclick?