JQuery表重新加载后JavaScript未执行

时间:2018-04-03 09:41:01

标签: javascript jquery html

我有一个为我重新加载表的函数,但是当我点击重新加载表时,它不执行其中的JavaScript。它确实刷新了。我只是暂时使用h3点击,因为它很容易。

<script>
        function RefreshTable()
         {
            $( "#calenderTable" ).load( "my-page.html #calenderTable" );
          }

        $(document).ready(function()
        {
          $("h3").click(function()
          {
            RefreshTable();
          });

        });
</script>

HTML

  <h3>Refresh Table</h3>

                                     <table class="table" id="calenderTable">

                                        <tr>
                                            <th  scope="col">House</th>
                                            <th scope="col">Monday <br><script type="text/javascript">document.write("Hello World");</script></th>
                                            <th scope="col">Tuesday <br><script type="text/javascript">document.write(getDate(2));</script></th>
                                            <th scope="col">Wednsday <br><script type="text/javascript">document.write(getDate(3));</script></th>
                                            <th scope="col">Thursday <br><script type="text/javascript">document.write(getDate(4));</script></th>
                                            <th scope="col">Friday <br> <script type="text/javascript">document.write(getDate(5));</script></th>
                                        </tr>

                                    <tbody>
                                        <tr>
                                            <td class="cal"><a href="#"> 1 </a></td>
                                            <td ></td>
                                            <td ></td>
                                            <td ></td>
                                            <td ></td>
                                            <td></td>
                                        </tr>
                                    </tbody>
                                </table>

所以第一次加载页面时会显示&#34; hello world&#34;但是,当我点击刷新它不再显示。我只是在这个例子中使用hello world,我真的希望getDate函数能够执行并在表格中显示正确的日期。

1 个答案:

答案 0 :(得分:0)

如果我理解

<script>
    function refreshTable() {
        $.ajax({
            url: 'my-page.html',
            type: 'GET',
            success: function (html) {
                $("#calenderTable").html($(html).find("#calenderTable").html());
            }
        });
    }

    $(document).ready(function () {
        $("h3").on('click', function () {
            refreshTable();
        });

    });
</script>