繁体   English   中英

如何在不重新加载页面的情况下刷新 div?

[英]How to refresh div without reloading page?

    <script>
        $(document).ready(function () {
            setInterval(function () {
                $("#result").load(window.location.href + " #result");
            }, 5000);
        });
    </script>

这是我的脚本代码

    <div class="container mb-3">
        <div class="jumbotron">
            <div id="result">
                <% for (var i = info.length - 1; i < info.length; i++){
                var curTemp = info[i].temp;
                var curGas = info[i].gas; %>
                <h2>temp : <%= curTemp %>C</h2>
                <h2>gas : <%= curGas %>ppm</h2>
                <% } %>
            </div>
        </div>
    </div>

什么是问题? 我想重新加载温度和气体值。 但它不起作用如何刷新那个div?

You are embedding whole request in the result div, instead, you can try getting required JSON output from ajax request and change the value on HTML so that it will not load the page or create new page request on the server

<script>
        $(document).ready(function () {
            setInterval(function () {
               //get data from ajax request which returns json and output in result div

                $("#result").load(window.location.href + " #result");
            }, 5000);
        });
    </script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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