1 Star 0 Fork 15

Xiao hui/TypeScript教程

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
script.js 2.59 KB
一键复制 编辑 原始数据 按行查看 历史
技术蛋老师 提交于 2022-10-04 18:11 . 完整代码
"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);
});
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhong-yaohui/type-script-tutorial.git
git@gitee.com:zhong-yaohui/type-script-tutorial.git
zhong-yaohui
type-script-tutorial
TypeScript教程
master

搜索帮助