2015-07-13 120 views
1

我想根據值更改每個單元格的背景顏色。但我不能讓它工作根據單元格值更改背景顏色

http://jsfiddle.net/qvp0n78w/2/

$(document).ready(function() {  
    var cell = $('table.maandrendementen td');  
    cell.each(function() {  
     var cell_value = $(this).html();  
     // Positief  
     if ((cell_value >= 0) && (cell_value <= 0,3)) { 
      $(this).css({ 'background' : '#7FFF95' });  
     } 
     else if ((cell_value >= 0,31) && (cell_value <= 0,5)) { 
      $(this).css({ 'background' : '#66FF7C' }); 
     } 
     else if ((cell_value >= 0,51) && (cell_value <= 0,7)) { 
      $(this).css({'background' : '#4DFF63'}); 
     } 
     else if ((cell_value >= 0,71) && (cell_value <= 1)) { 
      $(this).css({'background' : '#33F749'}); 
     } 
     else if ((cell_value >= 1,01) && (cell_value <= 1,5)) { 
      $(this).css({'background' : '#1ADE30'}); 
     } 
     else if (cell_value >= 1,5) { 
      $(this).css({'background' : '#00CC66'}); 
     } 

     // Negatief 
     else if ((cell_value >= -0,01) && (cell_value <= -0,2)) { 
      $(this).css({'background' : '#F6ADAC'}); 
     } 
     else if ((cell_value >= -0,31) && (cell_value <= -0,5)) { 
      $(this).css({'background' : '#F18483'}); 
     } 
     else if ((cell_value >= 0,51) && (cell_value <= -0,7)) { 
      $(this).css({'background' : '#EF706E'}); 
     } 
     else if ((cell_value >= -0,71) && (cell_value <= -1)) { 
      $(this).css({'background' : '#ED5B5A'}); 
     } 
     else if ((cell_value >= -1,01) && (cell_value <= -1,5)) { 
      $(this).css({'background' : '#EB4745'}); 
     } 
     else if (cell_value >= -1,5) { 
      $(this).css({'background' : '#E93331'}); 
     } 
    }); 
}); 

任何幫助,將不勝感激

+0

'0,3'不被認爲是在JS的數值。您需要在單元格的HTML中用'.'替換''',然後使用'parseFloat'將其轉換爲有效的數值,然後對其執行'> ='<='操作 –

回答

1

您需要使用數值比較,所以你需要將值轉換爲數字。由於您使用的是十進制值,因此在javascript中使用.表示法作爲小數分隔符,因此您需要使用replace()將,替換爲.,然後您需要從字符串中刪除%

$(document).ready(function() { 
 

 
    var cell = $('table.maandrendementen td'); 
 

 
    cell.each(function() { 
 
    var cell_value = +$(this).html().trim().replace(',', '.').replace('%', ''); 
 

 
    // Positief 
 

 
    if ((cell_value >= 0) && (cell_value <= 0.3)) { 
 
     $(this).css({ 
 
     'background': '#7FFF95' 
 
     }); 
 
    } else if ((cell_value >= 0.31) && (cell_value <= 0.5)) { 
 
     $(this).css({ 
 
     'background': '#66FF7C' 
 
     }); 
 
    } else if ((cell_value >= 0.51) && (cell_value <= 0.7)) { 
 
     $(this).css({ 
 
     'background': '#4DFF63' 
 
     }); 
 
    } else if ((cell_value >= 0.71) && (cell_value <= 1)) { 
 
     $(this).css({ 
 
     'background': '#33F749' 
 
     }); 
 
    } else if ((cell_value >= 1.01) && (cell_value <= 1.5)) { 
 
     $(this).css({ 
 
     'background': '#1ADE30' 
 
     }); 
 
    } else if (cell_value >= 1.5) { 
 
     $(this).css({ 
 
     'background': '#00CC66' 
 
     }); 
 
    } 
 

 
    // Negatief 
 
    else if ((cell_value >= -0, 01) && (cell_value <= -0, 2)) { 
 
     $(this).css({ 
 
     'background': '#F6ADAC' 
 
     }); 
 
    } else if ((cell_value >= -0, 31) && (cell_value <= -0, 5)) { 
 
     $(this).css({ 
 
     'background': '#F18483' 
 
     }); 
 
    } else if ((cell_value >= 0, 51) && (cell_value <= -0, 7)) { 
 
     $(this).css({ 
 
     'background': '#EF706E' 
 
     }); 
 
    } else if ((cell_value >= -0, 71) && (cell_value <= -1)) { 
 
     $(this).css({ 
 
     'background': '#ED5B5A' 
 
     }); 
 
    } else if ((cell_value >= -1, 01) && (cell_value <= -1, 5)) { 
 
     $(this).css({ 
 
     'background': '#EB4745' 
 
     }); 
 
    } else if (cell_value >= -1, 5) { 
 
     $(this).css({ 
 
     'background': '#E93331' 
 
     }); 
 
    } 
 

 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<table class="maandrendementen"> 
 
    <tr> 
 
    <th>jan</th> 
 
    <th>feb</th> 
 
    <th>mar</th> 
 
    <th>apr</th> 
 
    <th>may</th> 
 
    <th>jun</th> 
 
    <th>jul</th> 
 
    <th>aug</th> 
 
    <th>sep</th> 
 
    <th>oct</th> 
 
    <th>nov</th> 
 
    <th>dec</th> 
 
    </tr> 
 
    <tr> 
 
    <td>-0,23%</td> 
 
    <td>0,57%</td> 
 
    <td>0,39%</td> 
 
    <td>-1,24%</td> 
 
    <td>-0,59%</td> 
 
    <td>-1,37%</td> 
 
    <td>-0,85%</td> 
 
    <td>0,80%</td> 
 
    <td>1,94%</td> 
 
    <td>0,68%</td> 
 
    <td>-1,35%</td> 
 
    <td>2,69%</td> 
 
    </tr> 
 
</table>

0

首先,你需要刪除 '%' 符號,然後解析成浮點這一點。您還沒有在您的HTML代碼中添加'maandrendementen'類,但是您已經在jQuery選擇器中使用了它。

<table class="maandrendementen"> <!-- added class --> 
    <tr> 
    <th>jan</th> 
    <th>feb</th> 
    <th>mar</th> 
    <th>apr</th> 
    <th>may</th> 
    <th>jun</th> 
    <th>jul</th> 
    <th>aug</th> 
    <th>sep</th> 
    <th>oct</th> 
    <th>nov</th> 
    <th>dec</th> 
    </tr> 
    <tr> 
    <td>-0,23%</td> 
    <td>0,57%</td> 
    <td>0,39%</td> 
    <td>-1,24%</td> 
    <td>-0,59%</td> 
    <td>-1,37%</td> 
    <td>-0,85%</td> 
    <td>0,80%</td> 
    <td>1,94%</td> 
    <td>0,68%</td> 
    <td>-1,35%</td> 
    <td>2,69%</td> 
    </tr> 
</table> 

更改JavaScript這個(通知逗號改爲點的號碼 - 浮點數需要點作爲分隔符):

$(document).ready(function() { 

var cell = $('table.maandrendementen td'); 

cell.each(function() { 
//remove % and change to float 
var cell_value = parseFloat($(this).html().slice(0, -1)); 

// Positief 

if ((cell_value >= 0) && (cell_value <= 0.3)) { 
    $(this).css({'background-color' : '#7FFF95'}); 
} 
else if ((cell_value >= 0.31) && (cell_value <= 0.5)) { 
    $(this).css({'background-color' : '#66FF7C'}); 
} 
else if ((cell_value >= 0.51) && (cell_value <= 0.7)) { 
    $(this).css({'background-color' : '#4DFF63'}); 
} 
else if ((cell_value >= 0.71) && (cell_value <= 1)) { 
    $(this).css({'background-color' : '#33F749'}); 
} 
else if ((cell_value >= 1.01) && (cell_value <= 1.5)) { 
    $(this).css({'background-color' : '#1ADE30'}); 
} 
else if (cell_value >= 1.5) { 
    $(this).css({'background-color' : '#00CC66'}); 
} 

// Negatief 

else if ((cell_value >= -0.01) && (cell_value <= -0.2)) { 
    $(this).css({'background-color' : '#F6ADAC'}); 
} 
else if ((cell_value >= -0.31) && (cell_value <= -0.5)) { 
    $(this).css({'background-color' : '#F18483'}); 
} 
else if ((cell_value >= 0.51) && (cell_value <= -0.7)) { 
    $(this).css({'background-color' : '#EF706E'}); 
} 
else if ((cell_value >= -0.71) && (cell_value <= -1)) { 
    $(this).css({'background-color' : '#ED5B5A'}); 
} 
else if ((cell_value >= -1.01) && (cell_value <= -1.5)) { 
    $(this).css({'background-color' : '#EB4745'}); 
} 
else if (cell_value >= -1.5) { 
    $(this).css({'background-color' : '#E93331'}); 
} 

}); 

}); 

現在,它的工作 - 看http://jsfiddle.net/7pv3fw9d/

0

1。將字符串轉換爲數字

在比較之前,您必須將'1,6%'字符串解析爲數字, a var value = Number('1,6%'.replace(',', '.').replace('%', ''));

2.生成地圖上,而不是使用難看ifhttps://jsbin.com/keqepicine/edit?js,console,output

$(document).ready(function() { 

    var cell = $('table td'); 

    var minColor = 'red'; 
    var maxColor = 'green'; 

    var mapColor = [ 
     { 
     value: -2.0, 
     color: 'red' 
     }, 
     { 
     value: -0.1, 
     color: 'orange' 
     }, 
     { 
     value: 0.1, 
     color: 'grey' 
     }, 
     { 
     value: 0.2, 
     color: 'blue', 
     }, 
     { 
     value: 0.5, 
     color:'#66FF7C' 
     } 
    ]; 

    function getColor(value){ 
     value = + value.trim().replace(/,/g, '.').replace('%', ''); 


     if(value < mapColor[0].value){ 
     return minColor; 
     } 

     for(var i = 0; i<mapColor.length; i++){ 
      if(value < mapColor[i].value){ 
       return mapColor[i].color; 
      } 
     } 

     return maxColor; 

    } 

    cell.each(function() { 
     $(this).css('background-color', getColor($(this).html())); 
    }); 

}); 
相關問題