if (floatingBox && floatingBox.querySelector("table")) {
// 如果找到 table 元素,则删除
floatingBox.querySelector("table").remove();
}
// 创建悬浮框元素
var floatingBox = document.createElement("div");
floatingBox.id = "floating-box";
floatingBox.style.position = "fixed";
floatingBox.style.bottom = "20px";
floatingBox.style.right = "20px"; // 调整为右侧对齐
floatingBox.style.backgroundColor = "#fff";
floatingBox.style.padding = "10px";
floatingBox.style.border = "1px solid #ccc";
// 创建表格元素
var table = document.createElement("table");
table.style.borderCollapse = "collapse";
// 创建表头行
var tableHeaderRow = document.createElement("tr");
var headers = ["时间", "监控价格", "类型", "当前价格"];
headers.forEach(function(header) {
var th = document.createElement("th");
th.textContent = header;
th.style.border = "1px solid #ccc";
tableHeaderRow.appendChild(th);
});
table.appendChild(tableHeaderRow);
// 创建每一行数据
var dataRows = [
["2023-05-01 10:28:47", "12.9", "券后", "7.9"],
["2023-05-01 11:04:01", "12.9", "直接售价", "12.9"],
["2023-05-01 12:03:50", "12.9", "直接售价", "12.9"],
["2023-05-01 13:03:53", "12.9", "直接售价", "12.9"],
["2023-05-01 14:03:51", "12.9", "直接售价", "12.9"]
];
dataRows.sort(function(a, b) {
var timeA = new Date(a[0]);
var timeB = new Date(b[0]);
return timeB - timeA;
});
dataRows.forEach(function(rowData) {
var tr = document.createElement("tr");
rowData.forEach(function(cellData) {
var td = document.createElement("td");
td.textContent = cellData;
td.style.border = "1px solid #ccc";
tr.appendChild(td);
});
table.appendChild(tr);
});
// 将表格添加到悬浮框中
floatingBox.appendChild(table);
// 将悬浮框元素添加到页面中
document.body.appendChild(floatingBox);