新浪股票行情查询 (新浪股票行情中心)

访客 27 0
查询 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);});});

抱歉,评论功能暂时关闭!