新浪股票行情查询 (新浪股票行情中心)
访客
27
查询
document.querySelector("stock-search").addEventListener("submit", function(e) {e.preventDefault();// 获取股票代码或名称const stock = document.querySelector("stock-search input").value;// 构建新浪股票行情查询URLconst url = `${stock}`;// 发起请求fetch(url).then(response => response.text()).then(data => {// 解析新浪股票行情数据const [name, open, close, current, high, low] = data.split(",");const stockName = decodeURIComponent(name.substring(1)); // 解码股票名称const stockOpen = Number(open); // 转换开盘价const stockClose = Number(close); // 转换收盘价const stockCurrent = Number(current); // 转换当前价const stockHigh = Number(high); // 转换最高价const stockLow = Number(low); // 转换最低价const stockChange = stockCurrent - stockClose; // 计算涨跌幅// 创建表格const table = document.createElement("table");const tableHeader = "
名称 | 开盘价 | 收盘价 | 当前价 | 最高价 | 最低价 | 涨跌幅 |
---|
";const tableBody = `
${stockName} | ${stockOpen} | ${stockClose} | ${stockCurrent} | ${stockHigh} | ${stockLow} | ${stockChange.toFixed(2)} |
`;table.innerHTML = tableHeader + tableBody;// 将表格添加到结果区域document.querySelector("stock-results").appendChild(table);});});
版权声明:除非特别标注,否则均为本站转摘于网络,转载时请以链接形式注明文章出处。