用于更改特定文本数据的背景颜色的Jquery代码

时间:2018-06-11 15:32:18

标签: jquery css

我试图实现Jquery代码以改变背景颜色,而数据是特定的文本=" Habilitados"。我不能让它工作。我的代码出了什么问题?

var item ;
$(document).on('knack-view-render.view_292', function (event, scene) {
    var data = Knack.models['view_292'].data.models;
    for (x = 0; x < data.length; x++) {
        item = data[x].attributes;

        if(item.field_1062=="Habilitados") {
            $("#"+item.id).css("background-color", "red");
        }
    }
});

1 个答案:

答案 0 :(得分:0)

这应该对您有用(如果Knack更改其标记,则可能会中断)

// define your field values here
var colorMap = {
  "Habilitados": {
    "background_color": "red", 
    "color": "#fff"
  },
  // you can add additional values/colors here
};

// find a given field and map it's color based on values in the colorMap
function changeFieldColor(field, color_map){
  var child_field = $(field).find('.kn-detail-body');
  var value = child_field.text()

  if (color_map[value]) {
    $(child_field).css({'background-color' : color_map[value].background_color, 'color': color_map[value].color });
  }

}

// run this function on every page in your app
$(document).on('knack-scene-render.any', function() {
  changeFieldColor('.field_17', colorMapOne);
});