代码之家  ›  专栏  ›  技术社区  ›  AT-2017

下一条语句没有执行-jquery

  •  1
  • AT-2017  · 技术社区  · 6 年前

    我想向用户展示 页面加载 数据加载延迟时的功能。它的工作方式是相反的。假设一页中有一个表,如果表的长度为零,那么它将显示一个 页面加载 类特征 请稍候 . 所以我试过这样的方法 jQuery :

    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sweetalert2@7.29.1/dist/sweetalert2.all.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/promise-polyfill"></script>
    <script type="text/javascript">
                $(window).load(function () {
                    if ($('table').length != 0) { //Checking the table length here
                        swal.close(); //Close when data uploaded
                        console.log($('table').length);
                    }
                    else if ($('table').length == 0) {
                        swal.showLoading(); //Show when data upload delayed or zero
                        console.log($('table').length);
                    }
                });
    </script>  
    

    这很简单,但它没有显示 请稍候 在页面中最初上载数据时的功能。当我尝试相反的方法时,比如说,页面加载,然后这个功能就完美地工作了。我错过了什么吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Ryan RR    6 年前

    jquery的加载事件将在窗口中的所有元素完全加载之后触发。这就是为什么您的代码只有在加载已经完成时才能工作(当您反转它时)。

    $(window).load(function () {
    

    此函数中的所有内容都将在页面完全加载后触发。尝试这样做。

    if ($('table').length == 0) {
        swal.showLoading(); //Show when data upload delayed or zero
        console.log($('table').length);
    }
    
    $(window).load(function () {
          if ($('table').length != 0) { //Checking the table length here
              swal.close(); //Close when data uploaded
              console.log($('table').length);
          }
    });
    

    参考文献: https://api.jquery.com/load-event/