代码拉取完成,页面将自动刷新
同步操作将从 技术蛋老师/TypeScript教程 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const url = 'https://api.thecatapi.com/v1/images/search';
const button = document.querySelector('button');
const tableBody = document.querySelector('#table-body');
class Cat {
constructor(id, url, height, width) {
this.id = id;
this.url = url;
this.height = height;
this.width = width;
}
}
class WebDisplay {
static addData(data) {
const cat = new Cat(data.id, data.url, data.height, data.width);
const tableRow = document.createElement('tr');
tableRow.innerHTML = `
<td>${cat.id}</td>
<td><img src="${cat.url}" /></td>
<td>${cat.height.toString()}</td>
<td>${cat.width.toString()}</td>
<td>${cat.url}</td>
<td><a href="#">X</a></td>
`;
tableBody === null || tableBody === void 0 ? void 0 : tableBody.appendChild(tableRow);
}
static deleteData(deleteButton) {
const td = deleteButton.parentElement;
const tr = td.parentElement;
tr.remove();
}
}
function getJSON(url) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield fetch(url);
const json = yield response.json();
return json;
});
}
function getData() {
return __awaiter(this, void 0, void 0, function* () {
try {
const json = yield getJSON(url);
const data = json[0];
WebDisplay.addData(data);
}
catch (error) {
let message;
if (error instanceof Error) {
message = error.message;
}
else {
message = String(error);
}
console.log(error);
}
});
}
button === null || button === void 0 ? void 0 : button.addEventListener('click', getData);
tableBody === null || tableBody === void 0 ? void 0 : tableBody.addEventListener('click', (ev) => {
WebDisplay.deleteData(ev.target);
});
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。