上海股市指数行情 (上海股市指数是多少)

上海休闲 08-04 阅读:1 评论:18
上海股市指数行情 (上海股市指数是多少)
指数 当前值 涨跌幅 涨跌值
上证指数 3580.00 +1.23% +43.60
深证成指 15000.00 -0.89% -132.00
创业板指 3200.00 +2.11% +67.20
数据更新时间:2023-03-13 15:00:00
CSS样式表 (style.css): css body {font-family: Arial, sans-serif;font-size: 16px;line-height: 1.5;color: 333; }h1 {font-size: 24px;margin-bottom: 10px; }table {width: 100%;border-collapse: collapse; }th, td {border: 1px solid ccc;padding: 5px; }th {text-align: left;background-color: f2f2f2; }td {text-align: right; }update-time {font-size: 12px;color: 666;margin-top: 10px; }.positive {color: green; }.negative {color: red; }JavaScript脚本 (script.js): javascript // 获取 DOM 元素 const shIndex = document.getElementById('sh-index'); const shChange = document.getElementById('sh-change'); const shChangeValue = document.getElementById('sh-change-value'); const szIndex = document.getElementById('sz-index'); const szChange = document.getElementById('sz-change'); const szChangeValue = document.getElementById('sz-change-value'); const cybIndex = document.getElementById('cyb-index'); const cybChange = document.getElementById('cyb-change'); const cybChangeValue = document.getElementById('cyb-change-value'); const updateTime = document.getElementById('update-time');// 每 5 秒更新一次数据 setInterval(fetchData, 5000);// 获取上海股市指数行情数据 function fetchData() {fetch('https://hq.sinajs.cn/list=sh000001,sz399001,sz399006').then(response => response.text()).then(data => {// 解析响应数据const regex = /\"(.?)\"/g;const matches = data.match(regex);// 更新 DOM 元素shIndex.textContent = matches[0].slice(1, -1);shChange.textContent = matches[1].slice(1, -1);shChangeValue.textContent = matches[2].slice(1, -1);szIndex.textContent = matches[3].slice(1, -1);szChange.textContent = matches[4].slice(1, -1);szChangeValue.textContent = matches[5].slice(1, -1);cybIndex.textContent = matches[6].slice(1, -1);cybChange.textContent = matches[7].slice(1, -1);cybChangeValue.textContent = matches[8].slice(1, -1);// 更新更新时间const now = new Date();updateTime.textContent = `数据更新时间:${now.toLocaleDateString()} ${now.toLocaleTimeString()}`;// 根据涨跌幅设置颜色if (shChangeValue.textContent.startsWith('+')) {shChange.classList.add('positive');shChangeValue.classList.add('positive');} else {shChange.classList.add('negative');shChangeValue.classList.add('negative');}if (szChangeValue.textContent.startsWith('+')) {szChange.classList.add('positive');szChangeValue.classList.add('positive');} else {szChange.classList.add('negative');szChangeValue.classList.add('negative');}if (cybChangeValue.textContent.startsWith('+')) {cybChange.classList.add('positive');cybChangeValue.classList.add('positive');} else {cybChange.classList.add('negative');cybChangeValue.classList.add('negative');}}).catch(error => {console.error('获取数据失败:', error);}); }
版权声明

本文仅代表作者观点,不代表上海桑拿立场。
本文系作者授权发表,未经许可,不得转载。