jQuery的阿贾克斯加载图像,同时获取数据图像、加载、数据、jQuery

2023-09-10 16:03:58 作者:  说好的幸福呢∮

我使用一个jQuery的Ajax功能来从数据库中的数据,在点击事件。

I am using a Jquery Ajax function to fetch the data from database, on click event.

此功能工作正常,并显示数据。 但它需要时间来加载数据,所以我想提出一个加载图像,而数据被取出。 我用这个方法来做。

This function works fine and displays the data. But it takes time to load the data, so I want to put a loading image, while the data is been fetched. I used this method to do

<script type="text/javascript">
    jQuery(function($) {
        $(document).ready(function() {
            $("#imgs").hide();
            $(".letter").bind('click', function(){
                $("#imgs").fadeIn();
                var val = $(".letter").val;
                var site = '<?php echo site_url(); ?>';
                $.ajax({
                    url: site+'/wp-content/themes/premiumnews/newContent.php?letter='+$(this).html(), 
                    success:function(data){
                        $("#imgs").hide();
                        $("#content1").html(data);
                    }
                });
            });
        });
    });
</script>

当我按一下按钮,加载的形象出现,但成功后的图像应该消失,在我的情况下,它仍然有

When I click on the button, the loading image appears, but after success the image should disappear, in my case it still remains there

是我的code错了吗?或我使用了错误的方法?

Is my code wrong? or I have used a wrong method?

推荐答案

这是我一直在过去使用:

This is what I have always used in the past:

$('#loading_div').hide().ajaxStart(function(){
    $(this).show();
}).ajaxStop(function() {
    $(this).hide();
});