使用jquery为数组的每个值添加一些样式

时间:2016-04-04 08:31:28

标签: javascript jquery ajax

我会那样:如果我的状态等于"打开"我有背景颜色黄色或者如果它等于"关闭"我有背景颜色的红色等......但我没有成功,你可以帮助我吗?:

script.js:

   //fonction pour afficher les tickets de l'associé
function displaytickets(y){
    console.log(y);
    var newid = {};
    $("#mylist").empty();
    $("#nbtick").html("");
    $("#mycontent").html("");
    $("#name").html("");
    var newv = y;
    $.ajax({
      url: "https://cubber.zendesk.com/api/v2/users/"+y+"/tickets/requested.json?sort_by=date",
      type: 'GET',
      dataType: 'json',
      cors: true ,
            contentType:'application/json',
            secure: true,
            beforeSend: function (xhr) {
                xhr.setRequestHeader ("Authorization", "Basic " + btoa(""));
            },
            success: function (data){

        var sortbydate = data.tickets.sort(function(a,b){ return new Date(b.created_at)- new Date(a.created_at); });
        var named = data.tickets[0].via.source.from.name;

        localStorage.setItem("namestock", named);
                for (i = 0; i < data.tickets.length; i++) {

          var myticket = data.tickets[i];
          var mydate = data.tickets[i].created_at;
          var created = moment(mydate).format("MM-DD-YY");
                    var mytitle = data.tickets[i].subject;
            var description = data.tickets[i].description;
            var status = data.tickets[i].status;

            var myid = data.tickets[i].id;

            switch(status){
              case "open":
              console.log("open")
              $("")
              break;
              case "closed":
              console.log("closed")
              break;
              case "pending":
              console.log("pending")
              break;
              case "solved":
              console.log("solved")
              break;

            }

            localStorage.setItem("mydescription", description);
                      $("#mylist").append('<li class="row col-md-12" id="newlist" value="'+myid+'" onclick="ticketcontent('+myid+')">'+ '<div class="open">'+status+ '</div>'+'<div class="identifiant col-md-2">'+" #"+ myid +'</div>'+'<div class="identifiant col-md-6">'+mytitle +'</div>'+'<div class="identifiant col-md-2">'+created+'</div>'+'</li><hr><br>')
                }
      var nbticket = data.tickets.length;
      var monnom = localStorage.getItem("namestock");
      $("#name").append('<h2 class="title">'+monnom+"  "+nbticket+" ticket(s)"+'</h2>');
        localStorage.clear();
            },
  });
  $("#idisplay").css("display", "none");


}

在这里我得到的不是正确的颜色!:

enter image description here

1 个答案:

答案 0 :(得分:3)

点击 CSS

.class_closed{
  background-color:#ff0000;
}

.class_open{
  background-color:#fff000;
}

现在在 jQuery 中追加这个(假设状态是存储状态为打开或关闭的变量)

<span class="class_'+status+' otherClasses">' + status + '</span>