javascript - 将 Font Awesome 旋转齿轮添加到 jquery 点击功能?

标签 javascript jquery font-awesome

我需要在检索数据时添加 font awesome spinning cog,并在完成后将其删除。

这是要添加的 html:

<i class="fa fa-spin fa-cog"></i>

这是脚本:

(function($) {
  $('.acf-get-content-button').click(function(e){
    e.preventDefault();
    var $contentWrapper = $( '#acf-content-wrapper' );
    var postId = $contentWrapper.data( 'id' );

    $.ajax({
      url: "/field-ajax.php",
      type: "POST",
      data: {
        'post_id': postId
      },
    })
      .done(function( data ) {
        $contentWrapper.append( data );
        $('.acf-get-content-button').removeClass().addClass('.acf-get-content-button')
      });
  });
  $('.acf-get-content-button').mouseup(function(){
      if (event.which == 1) {
        $(".acf-get-content-button").hide();
      }
        });
})(jQuery);

我已经搜索了一段时间,但我发现的示例中没有一个看起来足够像我的脚本来实现它(我不是程序员)。

最佳答案

通过删除 fa-cogfa-spin 使图标保持“关闭”和“消失”状态,如下所示:

<i class="fa"></i>

添加以下内容:

$('.fa').addClass('fa-cog fa-spin')

$('.fa').removeClass('fa-cog fa-spin')

请参阅代码段以了解它们的位置。

引用: https://api.jquery.com/category/manipulation/class-attribute/

片段

(function($) {
  $('.acf-get-content-button').click(function(e) {
    e.preventDefault();
    $('.fa').addClass('fa-cog fa-spin');
    var $contentWrapper = $('#acf-content-wrapper');
    var postId = $contentWrapper.data('id');

    $.ajax({
        url: "/field-ajax.php",
        type: "POST",
        data: {
          'post_id': postId
        },
      })
      .done(function(data) {
        $('.fa').removeClass('fa-cog fa-spin');
        $contentWrapper.append(data);
        $('.acf-get-content-button').removeClass().addClass('.acf-get-content-button')
      });
  });
  $('.acf-get-content-button').mouseup(function() {
    if (event.which == 1) {
      $(".acf-get-content-button").hide();
    }
  });
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<i class='fa'></i>

关于javascript - 将 Font Awesome 旋转齿轮添加到 jquery 点击功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42141630/

相关文章:

html - 链接和图标之间的空间,fontawesome

javascript - D3(使用 Bootstrap 网格布局)- 工具提示放置已关闭

javascript - 如何从下拉列表复选框中删除所选项目

javascript - 如何使用对象属性从数组中删除对象 - React

JQuery - 按名称使用元素更改事件

javascript - 异步图片加载

xml - 带有超棒字体的 jsTree 树中的节点图标

javascript - 试图让 FontAwesome 与 jQuery Mobile 一起工作

javascript - 与 Ajax 一起使用的 Laravel Excel 无法正常工作

javascript - 捕获对 jquery 的异步响应