对于我的股票报价小工具,我试图动态设置每个股票代码的名称(GCF14.CMX ---> Gold)。
我的代码在这里:http://jsfiddle.net/thetuneupguy/JaCaH/1/
$(function () {
$.getJSON('http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20yahoo.finance.quotes%20WHERE%20symbol%20in(%22GCF14.CMX%22%2C%22SIF14.CMX%22%2C%22PAH14.NYM%22%2C%22PLF14.NYM%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=', function (data) {
console.log("data: ", data);
console.log(data.query.results.quote);
$.each(data.query.results.quote, function (key, obj) {
var obj = {};
obj['GCF14.CMX'] = Gold;
var changeClass = '';
var changeInPercentClass = '';
var $tr = $('<tr/>', {
'class': 'my-new-list'
}).appendTo('#blk-1 table');
$tr.append($('<td id="name" class="cells"/>').text(obj || "--"));
$tr.append($('<td class="cells"/>').text(obj.AskRealtime || "--"));
//$tr.append($('<td/>').text(obj.BidRealtime || "--"));
(obj.Change.substr(0,1) === '+') ? changeClass = 'green' : changeClass = 'red';
(obj.Change.substr(0,1) === '+') ? changeInPercentClass = 'green' : changeInPercentClass = 'red';
$tr.append($('<td class="'+changeClass+'">').text(obj.ChangeRealtime|| "--"));
var re = /([+|-]\d\.\d\d\%)/gi;
var rt = re.exec(obj.ChangePercentRealtime);
$tr.append($('<td class="'+changeInPercentClass+'">').text( rt[0] || "--"));
});
});
});
我一直在玩...
var obj = {};
obj['GCF14.CMX'] = Gold;
以及......
$tr.append($('<td id="name" class="cells"/>').text(obj || "--"));
答案 0 :(得分:0)
您正在获得的响应中已有一个名称字段...
不要这样做......
var obj = {};
obj['GCF14.CMX'] = Gold;
该代码获取包含所有信息的obj并将其设置为空白对象(破坏所有内容)。然后它尝试将obj ['GCF14.CMX']设置为等于未定义的对象。
你需要的只是
$tr.append($('<td id="name" class="cells"/>').text(obj.Name || "--"));
工作示例 - http://jsfiddle.net/JaCaH/3/