添加脚本代码的链接

时间:2016-01-10 16:36:34

标签: javascript jquery hyperlink window.open

我有这个脚本,现在有静态值:

var Lines = [];

var addLine = function(symbol, price, change, percent) {
  Lines.push('<tr>' +
    '<td class="symbol" >' + symbol  + '</td>' +
    '<td class="price"  >' + price   + '</td>' +
    '<td class="change" >' + change  + '</td>' +
    '<td class="percent">' + percent + '</td>' +
    '</tr>');
};

//  function to get data
function StockPriceTicker() {
  var Symbol = "",
      CompName = "",
      Price = "",
      ChnageInPrice = "",
      PercentChnageInPrice = "";

  var CNames = "TSLA,HPQ,VRSK,CERN,KHC,EXPD";
  var flickerAPI = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22" + CNames + "%22)&env=store://datatables.org/alltableswithkeys";

  var StockTickerXML = $.get(flickerAPI, function(xml) {
    $(xml).find("quote").each(function() {
      Symbol = $(this).attr("symbol");
      $(this).find("Name").each(function() {
        CompName = $(this).text();
      });
      $(this).find("LastTradePriceOnly").each(function() {
        Price = $(this).text();
      });
      $(this).find("Change").each(function() {
        ChnageInPrice = $(this).text();
      });
      $(this).find("PercentChange").each(function() {
        PercentChnageInPrice = $(this).text();
      });

      var PriceClass = "GreenText",
          PriceIcon = "up_green";

      if (parseFloat(ChnageInPrice) < 0) {
        PriceClass = "RedText";
        PriceIcon = "down_red";
      }


      var htmlSymbol,
          htmlPrice,
          htmlChange,
          htmlPercent;


      htmlSymbol = "<span class='quote'>" + Symbol + " </span></span>";
      htmlPrice = "<span class='" + PriceClass + "'>";
      htmlPrice = htmlPrice + parseFloat(Price).toFixed(2) + " ";

      htmlChange = parseFloat(Math.abs(ChnageInPrice)).toFixed(2) + "<span class='" + PriceIcon + "'></span>";

      htmlPercent = parseFloat(Math.abs(PercentChnageInPrice.split('%')[0])).toFixed(2) + "%";


      // use here the function defined above.
      addLine(htmlSymbol, htmlPrice, htmlChange, htmlPercent);

    });



    $body.empty().html(Lines.join(''));

    // we reset the content of Lines for the next interval
    Lines = [];

  });
}   

这里是running code script

我想要完成的是添加一个链接到每个&#34;符号&#34;包含在&#34; Cnames&#34;变种。 该链接类似于&#34; https://www.example.com/chart/?symbol=Ticker&#34;,其中&#34; Ticker&#34;是&#34;符号&#34;我在那一刻点击了。

例如,当我点击TSLA时,我希望它会打开一个弹出窗口(如window.open函数),该窗口完全指向&#34; https://www.example.com/chart/?symbol=TSLA&#34;。
除了&#34; =&#34;之后的最后一个单词,每个人的链接都是相同的。符号。
是否有可能向我展示可以做到的一部分代码?

1 个答案:

答案 0 :(得分:1)

我想你需要这个:

'<td class="symbol" > <a href="https://www.example.com/chart/?symbol=' + ($(symbol).text())  + '" target="_blank">' + symbol + '</a> </td>'