2010-09-15 65 views
0

我从YQL抓取一些JSON,将它解析成列表。然后我想根据其内容突出显示列表中的行。解析json,然后用它做点什么:如何获得正确的订单?

我可以通过在.click上突出显示来单独做这两件事情,但我真的不想这样做 - 我希望突出显示立即发生。下面是两位:

var yqlURL="http://query.yahooapis.com/v1/public/yql?q=select%20name%2Cstatus%2Ccurrentposition%2Ccashonhand%20from%20atom%20where%20url%3D'https%3A%2F%2Fspreadsheets.google.com%2Ffeeds%2Flist%2F0AiMSzYWiIFeldHdzUXBnWkJxaWZ1eEdvRm5LMUppYXc%2Fod6%2Fpublic%2Fvalues'&format=json&diagnostics=true&callback=?"; 

$.getJSON(yqlURL, function(data) { 

$.each(data.query.results.entry,function(){ 
$('#result').append('<li><b>'+this.name+'</b> &nbsp current job: '+this.currentposition+' | cash on hand: '+this.cashonhand+' | race status: <span class="status">'+this.status+'</span></li>'); 
}); 
}); 

OK,所以这是位一个

,这里是位二:

$('#clickall').click(function(){ 
$("#result li:contains('In')").toggleClass("in"); 
$("#result li:contains('Mulling')").toggleClass("mulling"); 
$("#result li:contains('Rumored')").toggleClass("rumored"); 
$("#result li:contains('Out')").toggleClass("out"); 
}); 

所以,是的,我知道必须有这样做的亮点的方法(这是什么在那里被切换),一旦JSON进入。但是,好吧,我是愚蠢的。

回答

2

做这种方式,

var yqlURL = "http://query.yahooapis.com/v1/public/yql?q=select%20name%2Cstatus%2Ccurrentposition%2Ccashonhand%20from%20atom%20where%20url%3D'https%3A%2F%2Fspreadsheets.google.com%2Ffeeds%2Flist%2F0AiMSzYWiIFeldHdzUXBnWkJxaWZ1eEdvRm5LMUppYXc%2Fod6%2Fpublic%2Fvalues'&format=json&diagnostics=true&callback=?"; 

$.getJSON(yqlURL, function(data) { 

    $.each(data.query.results.entry, function() { 
     var li = $('<li>').html('<b>' + this.name + '</b> &nbsp; current job: ' + this.currentposition + ' | cash on hand: ' + this.cashonhand + ' | race status: <span class="status">' + this.status + '</span>'); 

     $('#result').append(li); 

     if (this.status == 'In') { 
      li.addClass('in'); 
     } else if (this.status == 'Mulling') { 
      li.addClass('mulling'); 
     } /* else if (blah == blah) {bla blah}*/ 
    }); 

});​ 

quick demo

+0

OH MY GOD,我可以吻你! – 2010-09-15 01:56:29

+0

nahhh,一个投票会做...;) – Reigel 2010-09-15 02:00:49

0

您需要在调用getJSON回调后拨toggleClass调用。