ajax 全局loading 配置 ajax 正在加载中

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .loading{
            display: none;
            color: #f00f00;
        }
    </style>
</head>
<body>
<div class="root">
    <!--先定义loading CSS隐藏-->
    <div class="loading">正在加载中....</div>
</div>
</body>

<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script>
    $(function () {
        $(document).ajaxStart(function () {
            // ajaxStart() 方法规定 AJAX 请求开始时运行的函数。
            $('.loading').show()
        }).ajaxStop(function () {
            // ajaxStop() 方法规定 AJAX 请求结束时运行的函数。
            $('.loading').hide()
        })
        //示例测试
       $.ajax({
           type:'get',
           url:'www.baidu.com',
           success:function (data) {
               console.log(data)
           }
       })

    })
</script>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_38201500/article/details/85256100