0

我能够从 Google Finance 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

4

1 回答 1

1

Facebook (FB) 和 USMD 不在 NYSE,它们在 NASDAQ

这是将返回 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)) 中执行类似的操作; (使用您的小提琴示例)

于 2013-11-01T12:59:11.520 回答