javascript - jquery .attr() 类开关不起作用

标签 javascript jquery html css

我正在开发一个网络服务,

当用户通过 ajax 请求更多数据时,我希望包含数据的 div 显示一个加载圆圈。当我想要圆圈时,我在样式文件中写了一个 css 类:

.ajax_getTeams_loading{

background: url('ajax-loader.gif') no-repeat center center;

 }

所以我的ajax函数是这样的:

        function(profile, divId){



    $('#' + divId).attr('class', 'goose');


    /*$.get('testGetTeams.php', {username: profile}, function(data) {
        $('#' + divId).html(data);
    });*/

}

问题是圆圈永远不会出现。我尝试将 css 类简化为背景颜色:蓝色,但这甚至没有用。我也完全删除了 ajax 部分,它仍然根本不起作用。我究竟做错了什么?

最佳答案

此时我没有看到您将“ajax_getTeams_loading”类添加到 div。但是,因为它是一个背景,所以如果 div 中有数据,您可能看不到它。然后它将位于文本下方或 div 中的任何内容。

最好用加载图标替换 div 中的任何内容,然后用新请求的数据替换加载图标。示例:

// store your div object in a variable,
// this is faster since you'll be using it multiple times
var div = $('#' + divId);

// replace the div's content with the loader
// you might want to add a width and height to the css
// to make sure the div is large enough to show the entire loading icon
var loader = '<div class="ajax_getTeams_loading"></div>';
div.html(loader);

$.get('testGetTeams.php', {username: profile}, function(data) {
  // replace the loader icon with the requested data
  div.html(data);
});

关于javascript - jquery .attr() 类开关不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3901072/

相关文章:

javascript - 如何使用 Html 帮助程序在 mvc 中显示密码隐藏字段

javascript - 使用地理位置获取城市名称

javascript - 播放和提示/预加载JavaScript中的声音

javascript - 如果有人使用 jquery 选择美国,则需要更多输入

html - 创建论坛样式消息所需的 css/html

javascript - 使用变量访问未定义的数组结果

Javascript 反向排序

javascript - 使用 jQuery one() 函数作为 hack 来防止触发多个点击事件

javascript - 为什么我的脚本忽略空数组结果?

html - 当浏览器增加/减少时,布局背景和前景不会以相同的速率调整