[Typescript] Type JQuery
jQuerytypescript type
2023-09-14 09:00:45 时间
const $: any = {}
/**
* Get the <button> element with the class 'continue'
* and change its HTML to 'Next Step...'
*/
$("button.continue").html("Next Step...")
/**
* Show the #banner-message element that is hidden with visibility:"hidden" in
* its CSS when any button in #button-container is clicked.
*/
const hiddenBox = $("#banner-message")
$("#button-container button").on("click", (event) => {
hiddenBox.show()
})
/**
* Make an API call and fill a <div id="post-info">
* with the response data
*/
$.ajax({
url: "https://jsonplaceholder.typicode.com/posts/33",
success: (result) => {
$("#post-info").html(
"<strong>" + result.title + "</strong>" + result.body
)
},
})
.
.
.
import fetch from "node-fetch";
function isHTMLElement(x: HTMLElement | Element | null): x is HTMLElement {
return x !== null && x instanceof HTMLElement;
}
class SelectorResult {
#elements;
constructor(elements: NodeListOf<Element>) {
this.#elements = elements;
}
html(contents: string) {
this.#elements.forEach((elm) => {
elm.innerHTML = contents;
});
}
on<K extends keyof HTMLElementEventMap>(
arg0: K,
arg1: (event: HTMLElementEventMap[K]) => void
) {
this.#elements.forEach((elm) => {
if (isHTMLElement(elm)) {
elm.addEventListener(arg0, arg1);
}
});
}
show() {
this.#elements.forEach((elm) => {
if (isHTMLElement(elm)) {
elm.style.visibility = "visible";
}
});
}
hide() {
this.#elements.forEach((elm) => {
if (isHTMLElement(elm)) {
elm.style.visibility = "hidden";
}
});
}
}
function $(selector: string) {
return new SelectorResult(document.querySelectorAll(selector));
}
namespace $ {
export function ajax({
url,
success,
}: {
url: string;
success: (data: any) => void;
}) {
return fetch(url)
.then((res) => res.json())
.then((res) => {
success(res);
return res;
})
.catch((err: unknown) => {});
}
}
export default $;
相关文章
- Jquery - 获取所有子节点 ( 并删除 )
- JS原生方法实现jQuery的ready()
- 解密jQuery内核 DOM操作
- 使用jQuery Tools scrollable注意事项
- JQuery中eq()和get()的区别
- Json对象与Json字符串互转(4种转换方式) jquery 以及 js 的方式
- jquery 添加html元素后 html中click失效问题
- Jquery焦点图/幻灯片效果 插件 KinSlideshow
- 用一句JQuery代码实现表格的简单筛选
- jQuery-可折叠的模式
- jquery自动监测输入字数,自动截取,输入框
- Uncaught ReferenceError: $ is not defined之解决jquery引用不成功的办法
- jQuery UI 实例 - 标签页(Tabs)
- jquery 中一些 特殊方法 的特殊使用 一览表
- 黑马day16 jquery入门