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