显示Google财经股票API的股票价格总和

时间:2013-10-31 16:55:49

标签: javascript jquery json google-finance google-finance-api

我可以从Google财经API中提取和显示股票价格信息,但我想添加股票价格并显示合计金额的总和。给出以下代码:

HTML

<link rel="stylesheet" type="text/css" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.0-rc2/css/bootstrap.css">
 <script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script>
  <body>
  <div class="container">
  <h1>Stock Portfolio</h1>
  <h2>Total Portfolio Value:</h2>
  <div class="total"></div>
  <h3>Stocks</h3>
  <div class="stockTick"></div>
  <div class="stockTick2"></div>
</div>

的jQuery

$(document).ready(function(){

  $.getJSON('https://finance.google.com/finance/info?client=ig&q=NYSE:FB&callback=?',function(response){
    var stockInfo = response[0];
    var stockString =
   '<div class="stockWrapper">';
      stockString +='Company: <span class="stockSymbol"><b>'+stockInfo.t+'</b></span><br />';
      stockString +='Price: <span class="stockPrice">'+stockInfo.l+'</span><br />';
      stockString +='Change: <span class="stockChange">'+stockInfo.c+'</span><br />';
      stockString +=' Reported at: <span>at</span> <span class="stockTime">'+stockInfo.ltt+'</span><hr>';
    stockString +='</div>';
    $('.stockTick').prepend(stockString);
  });
  $.getJSON('https://finance.google.com/finance/info?client=ig&q=NYSE:USMD&callback=?',function(response){
    var stockInfo = response[0];
    var stockString2 =
   '<div class="stockWrapper">';
      stockString2 +='Company: <span class="stockSymbol"><b>'+stockInfo.t+'</b></span><br />';
      stockString2 +='Price: <span class="stockPrice">'+stockInfo.l+'</span><br />';
      stockString2 +='Change: <span class="stockChange">'+stockInfo.c+'</span><br />';
      stockString2 +=' Reported at: <span>at</span> <span class="stockTime">'+stockInfo.ltt+'</span>';
    stockString2 +='</div>';
    $('.stockTick2').prepend(stockString2);
  });
});

这是jsfiddle

1 个答案:

答案 0 :(得分:1)

FaceBook(FB)和USMD不在纽约证券交易所上市,他们在纳斯达克上市

以下是将返回JSON数据的Url。

https://finance.google.com/finance/info?client=ig&q=NASDAQ:FB https://finance.google.com/finance/info?client=ig&q=NASDAQ:USMD

要获得股票价格的总和,您可以在响应函数中执行此类操作 $('。total')。text(parseFloat($('#total')。text())+ parseFloat(stockInfo.l)); (使用你的小提琴例子)