0

我有以下代码

$.get('feed.txt', function(data) {
var arr = data.split('|');
var values = '';
for(var i=0; i<arr.length; i++){
  values += '<div>'+arr[i]+'</div>';
}
$('div').html(values);
});

我想根据文本文件(下面的示例)提供的信息向生成的 div 添加一个类,即如果 type="small" 那么我想添加 class="small"

TYPE: Small, ID: 0001, RESPONSE DATE AND TIME: 2012-12-12 12:00, UNITNAME: Name, MEMO: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.|
TYPE: Medium, ID: 0002, RESPONSE DATE AND TIME: 2012-12-12 01:00, UNITNAME: Name, MEMO: Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.|
TYPE: Large, ID: 0003, RESPONSE DATE AND TIME: 2012-12-12 02:00, UNITNAME: Name, MEMO : Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.|
4

1 回答 1

1
$.get('feed.txt', function(data) {
    var arr = data.split('|');
    var values = '';
    var type, className;
    for(var i=0; i<arr.length; i++){
        type = arr[i].split(',')[0];
        switch(type){
            case "TYPE: Small":
                className = 'small';
            break;
            case "TYPE: Medium":
                className = 'medium';
            break;
            case "TYPE: Large":
                className = 'large';
            break;
        }
        values += '<div class="'+className+'">'+arr[i]+'</div>';
    }
    $('div').html(values);
});
于 2012-12-11T03:47:39.673 回答