0
我比較兩個值設置bij數據屬性(res和ref)在一個div中,並基於那個我想設置div的bgcolor。不知何故,它不起作用。沒有bgcolor設置爲結果。比較div的設置bgcolor的值
的Html
<div id="grid">
<div class="item blue" data-kpi="one" data-res="8" data-ref="4" ></div>
<div class="item yellow" data-kpi="two" data-res="4" data-ref="7" data-groups='["all", "numbers", "green", "square"]'> </div>
<div class="item blue" data-kpi="three" data-res="8" data-ref="13"></div>
</div>
jQuery的
$('#grid div.item[data-res][data-ref]').each(function(index){
var res = $(this).data('res');
var ref = $(this).data('ref');
if(res > ref)
{
$(this).css('background-color', 'red');
}
else if(ref > res)
{
$(this).css('background-color', 'green');
}
else
{
$(this).css('background-color', 'black');
}
});
工作正常位置:: http://jsfiddle.net/U3Dpn/ –
順便說一句,你把它包在$(文件)。就緒(函數(){...}); –