将类添加到文本(jQuery)

时间:2017-05-25 21:02:42

标签: jquery ajax

我正在尝试仅在文本值为("table-danger")的位置添加类。但似乎即使选择它也行不通。我正在尝试将课程td添加到No rating found,其中文字为$( document ).ready(function() { $(".form-inline").submit(doStuff); }); function doStuff(e){ e.preventDefault(); var input = $("#nanizankaInput").val(); var link = "http://api.tvmaze.com/search/shows?q="; link = link + input; $.ajax({ url: link, method: 'GET', success: function (data) { console.log(data); for(var i = 0; i < data.length; i++){ $("thead").first().append( "<tr>" + "<td>" + data[i].show.id + "</td>" + "<td>" + data[i].show.name + "</td>" + "<td>" + data[i].show.rating.average + "</td>" +"</tr>"); } $("thead tr td").each(function(){ if($(this).text() == 'null') $(this).text('No rating found'); }); if ($("thead tr td:nth-child(3)").text() == 'No rating found'){ $("thead tr td:nth-child(3)").addClass("table-danger") } }}); }

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim CCell As Range
    Dim sht As Worksheet
    Set CCell = Range("L:L")
    Set sht = Worksheets("Sheet1") 'EDIT
    If Target.Count = 1 Then
        If Not Application.Intersect(CCell, Range(Target.Address)) _
            Is Nothing Then
                If Target.Value = "YES" Then
                    sht.Cells(Target.Row, 2).Interior.Color = RGB(255, 0, 0)
                End If
        End If
    End If
End Sub
  

直播演示 http://jsbin.com/pimitijigu/edit?html,js,output

1 个答案:

答案 0 :(得分:1)

要执行您所需的操作,您只需将addClass()调用放在each()循环中即可检查null单元格中的td值,例如这样:

$("thead tr td").each(function() {
  if ($(this).text() == 'null')
    $(this).text('No rating found').addClass('table-danger');
});

Working example

  

我想将它添加到整行

在这种情况下,你甚至可以修改附加HTML的循环,以便在一次传递中执行逻辑。试试这个:

for (var i = 0; i < data.length; i++) {
  var rating = data[i].show.rating.average, ratingClass = '';
  if (!rating) {
    rating = 'No rating found';
    ratingClass = 'table-danger';
  }

  $("thead").first().append('<tr class="' + ratingClass + '"><td>' + data[i].show.id + '</td><td>' + data[i].show.name + '</td><td>' + rating + '</td></tr>');
}

Working example