我有以下代码
$.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.|
答案 0 :(得分: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);
});