jQuery重新加载DIV? (烧瓶/金甲2) [英] JQuery to reload DIV? (Flask/Jinja2)

查看:47
本文介绍了jQuery重新加载DIV? (烧瓶/金甲2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索无济于事.我想我的问题有点独特.我有一个DIV,其中包含一些内容,当前是通过将SQL查询的对象传递到模板中来引入该内容的.效果很好,但是不允许我每隔x秒更新一次内容,而不刷新整个页面.我不想做的事.所以我想知道我可以迫使JQuery重新加载我的DIV元素吗?这还会重新加载其中的代码吗?因为那样会迫使我的div更新.这是代码.

I've searched to no avail.. I guess my question is a bit unique. I have a DIV with some content in it, that content get's brought in currently by passing the object of a SQL query into the template. That works fine, but doesn't allow me to update the content every x seconds, without refreshing the entire page. Something I don't want to do. So I wanted to know I could force JQuery to reload my DIV element? Would this also reload the code in it? Because that would force my div to update.. Here is the code.

<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中的内容,但如果可能的话,宁愿不这样做.

Is it possible to get JQuery to just reload that DIV with the code that is already there? If so that should fix my issue. I have thought about trying to create a call to get the content in JSON but IF possible would rather not.

推荐答案

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 }}!

这篇关于jQuery重新加载DIV? (烧瓶/金甲2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆