我如何使用javascript

时间:2019-07-03 21:56:27

标签: javascript html

我尝试过滤信息,但是我只需要刷新表并使用javascript显示信息,就找不到一个很好的方法

我尝试在我的页面上使用此代码,但我不知道如何实现

JS

    let url="https://server.test-cors.org/server?id=2934825&enable=true&status=200&credentials=false&methods=GET"

    async function refresh() {
          btn.style.visibility= "hidden";
          dynamicPart.innerHTML="Loading..."
          dynamicPart.innerHTML=await(await fetch(url)).text();
          setTimeout(refresh,2000);}

方法的HTML

HTML
<div id="staticPart">Here is static part of page
<button id="btn" onclick="refresh()">Start refreshing (2s)</button>
</div>

<div id="dynamicPart">Dynamic part</div>

这是我在HTML中的表格

<div class='d-flex justify-content-center' id="contentTable">
            <table class='table table-stripped' border=1 id="table">
                <thead class= 'thead-dark'>
                    <tr>
                        <th>#</th>
                        <th>Fecha</th>
                        <th>Documento</th>
                        <th>Nombres</th>
                        <th>Apellidos</th>
                        <th>Sede</th>
                        <th>Siglas</th>
                        <th>Administrador</th>                        
                        <th>Acciones</th>                        
                    </tr>
                </thead>
                <tbody>
                    <a href="/Jomar/users_control/controller/ControlAsisController.php?action=todos"></a>
                    <?php
                        for($i = 0; $i < count($datos); $i++){
                            $id = $datos[$i]['id_controlAsis'];
                            $rutaEd = "/Jomar/users_control/controller/ControlAsisController.php?action=editar&id_controlAsis=$id";
                            $rutaEl = "/Jomar/users_control/controller/ControlAsisController.php?action=eliminar&id_controlAsis=$id";
                            echo "<tr>";
                            echo "<td>". ($i+1) ."</td>";
                            echo "<td> {$datos[$i]['fecha']} </td>";
                            echo "<td> {$datos[$i]['documento']} </td>";
                            echo "<td> {$datos[$i]['nombres']} </td>";
                            echo "<td> {$datos[$i]['apellidos']} </td>";
                            echo "<td> {$datos[$i]['sede']} </td>";
                            echo "<td> {$datos[$i]['siglas']} </td>";
                            echo "<td> {$datos[$i]['administrador']} </td>";                            
                            echo "<td>
                            <a href='$rutaEd' class='badge badge-primary'>Editar </a>
                            <a href='$rutaEl' class='badge badge-warning'>Eliminar </a>
                            </td>";
                            echo "</tr>";
                        }

                    ?>
                </tbody>
            </table>
        </div>

感谢您的帮助,对不起我的英语

1 个答案:

答案 0 :(得分:0)

做出很多假设。使用它们之前,您需要定义'btn'和'dynamicPart'变量。

示例:

let btn = document.querySelector("#id_of_some_button_in_html");
let dynamicPart = document.querySelector("#id_of_a_div");

那你只需要打电话

refresh();

然后

setTimeout(refresh,2000);

您的代码将每2秒运行一次。

let url="https://server.test-cors.org/server?id=2934825&enable=true&status=200&credentials=false&methods=GET"

async function refresh() {
      let btn = document.querySelector("#id_of_some_button_in_html");
      let dynamicPart = document.querySelector("#id_of_a_div");

      btn.style.visibility= "hidden";
      dynamicPart.innerHTML="Loading..."
      dynamicPart.innerHTML=await(await fetch(url)).text();
      setTimeout(refresh,2000);}

refresh();

了解这不是“好的”代码,但我假设您只是开始学习JS,并且此代码不会投入生产。