如何在.load函数中添加加载文本?

时间:2015-10-29 11:35:05

标签: jquery ajax

我有这个jquery函数来加载基于href链接的页面:

<script type="text/javascript">
    $(document).ready(function(){
        $("#test a").click(function( e ){
            e.preventDefault();
            var href = $( this ).attr('href');
            $("#content").load( href , function(){
            $('#content').hide().fadeIn(500);
            });
        });
    });
    </script>

我的问题是,如何在加载href页面时显示加载div?

<div id=loading>LOADING...</div>
谢谢你的朋友们!

2 个答案:

答案 0 :(得分:1)

您可能会这样:

<script type="text/javascript">
$(document).ready(function(){
    $("#test a").click(function( e ){
        e.preventDefault();
        $("#loading").html("Loading");
        var href = $( this ).attr('href');
        $("#content").load( href , function(){
        $('#content').hide().fadeIn(500);
        });
       $("#loading").html("");
    });
});
</script>

答案 1 :(得分:1)

默认隐藏loading div:

#loading{
    display: none;
}

然后,在调用load之前显示它并将其隐藏在回调函数中。

....
var href = $( this ).attr('href');
$("#loading").show();
$("#content").load( href , function(){
        $("#loading").hide();
....
相关问题