【问题标题】:JavaScript not executing after JQuery table reload重新加载 JQuery 表后 JavaScript 未执行
【发布时间】:2018-04-03 09:41:01
【问题描述】:

我有一个为我重新加载表格的函数,但是当我单击重新加载表格时不会执行其中的 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>

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

【问题讨论】:

  • 我在您给定的 HTML 中看不到任何 h3 标记
  • @AlivetoDie 这不是问题,当我点击它时它会重新加载。
  • 我们如何检查?也添加那个 HTML 好友
  • 好的,我也添加了 h3 和 table id

标签: javascript jquery html


【解决方案1】:

如果我明白了

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

【讨论】:

  • 所以由于某种原因,只加载了 hello world 而没有任何表格或页面内容,我想要做的是刷新表格并执行其中的脚本。
  • html代码可能无效,是否有
    content
    有效?
猜你喜欢
  • 2019-05-02
  • 2014-10-02
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 1970-01-01
  • 2015-04-11
  • 1970-01-01
  • 2021-06-02
相关资源
最近更新 更多