繁体   English   中英

jQuery重新加载DIV? (烧瓶/金贾2)

[英]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.

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