【问题标题】:How to change fontawsome class name with ajax?如何使用 ajax 更改字体真棒类名?
【发布时间】:2021-07-30 20:46:22
【问题描述】:

我想在删除行后更改图标。我应该只在刀片中使用一次字体真棒图标,然后删除类名,然后用 ajax 添加类名吗?

刀片:

<div id="status-{{ $country->id }}">
  <div id="icon-{{$country->id}}">
    @if( getStatus($country->status) == 'Active' || getStatus($country->status) == 'Aktif' )
      <i class="fa fa-check-circle text-success"></i>
    @elseif( getStatus($country->status) == 'Inactive' || getStatus($country->status) == 'Pasif' )
      <i class="fas fa-minus-circle text-info"></i>
    @else
      <i class="fas fa-times-circle text-danger"></i>
    @endif
    <strong>{{ getStatus($country->status) }}</strong>
  </div>
</div>

ajax:

$.ajax({
    url: url,
    type: "DELETE",
    header: {
        _token: "{{ csrf_token() }}",
    },
    success() {
        toastr['error']("{{ __('home.delete.message') }}");
        swal("{{ __('home.delete.message') }}", {
            icon: "success",
        });
        $('#status-' + id).text('Deleted')
        $('#icon-' + id).attr('i').addClass('fas fa-times-circle text-danger')
    },
    error(error) {
        toastr['warning']("{{ __('home.error.message') }}");
    }
});

【问题讨论】:

  • find('i')代替attr('i')

标签: javascript html ajax laravel


【解决方案1】:

JQuery 的.attr() 函数用于改变元素的属性。它与它的孩子无关。

替换

$('#icon-' + id).attr('i').addClass('fas fa-times-circle text-danger')

有了这个

$('#icon-' + id + ' i').eq(0).attr('class', 'fas fa-times-circle text-danger');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2015-12-01
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多