dom-utils
2024-10-20 08:45:33
function isNil(obj:any): boolean {
return typeof obj === "undefined" || obj === null;
}
function fixCls(clsStr: string): string {
if (!clsStr) {
return "";
}
let clsArray = clsStr.split(" ");
clsArray = clsArray.filter((cls) => {
return cls.length > 0;
});
if (clsArray.length === 0) {
return "";
}
clsArray = clsArray.map((cls) => {
if (cls.startsWith("next")) {
return cls;
}
return `SOME_PREFIX_${cls}`;
})
return clsArray.join(" ");
}
const html2EscapeEnums: Record<string, string> = {
'<': '<',
'>': '>',
'&': '&',
'"': '"'
};
function html2Escape(sHtml: string) {
return sHtml.replace(/[<>&"]/g, function (c) {
return html2EscapeEnums[c] || c;
});
}
function appendChild(element: HTMLElement, child: any) {
if (isNil(child)) {
return;
} else if (typeof child === "string") {
const txt = html2Escape(child);
const oTxt = document.createTextNode(txt);
element.appendChild(oTxt);
return;
} else if (child instanceof HTMLElement) {
element.appendChild(child);
return;
} else {
const oTxt = document.createTextNode('' + child);
element.appendChild(oTxt);
}
}
function createElement(tagName: string, clazz: string, children?: any) {
const element = document.createElement(tagName);
element.className = fixCls(clazz);
if (!isNil(children)) {
if (Array.isArray(children)) {
for (let i = 0; i < children.length; i++) {
const child = children[i];
appendChild(element, child);
}
} else {
appendChild(element, children);
}
}
return element;
}
function div(clazz: string, children?: any): HTMLElement {
return createElement('div', clazz, children);
}
function span(clazz: string, children?: any): HTMLElement {
return createElement('span', clazz, children);
}
function img(clazz: string, src: string): HTMLElement {
const img = createElement('img', clazz);
img.setAttribute('src', src);
return img;
}
function ifElse(c: boolean, arr: HTMLElement[]): HTMLElement | null {
const a = isNil(arr[0]) ? null : arr[0];
const b = isNil(arr[1]) ? null : arr[1];
return c ? a : b;
}
export {
span,
div,
img,
ifElse
}
最新文章
- linux磁盘分区模式
- btrfs-snapper 实现Linux 文件系统快照回滚
- bzoj1067 降雨量&;&;vijos1265 暴风雨
- 《Linux内核分析》第六周 进程的描述与创建
- 非常全面的java基础笔试题
- nginx 3.nginx+fastcgi
- C# Base64解码 二进制输出
- C#模板打印功能-模板为WPS或Excel
- Python 3 使用venv创建虚拟环境
- POJ3155 Hard Life [最大密度子图]
- DB2开发系列之四——触发器
- bzoj5107: [CodePlus2017]找爸爸
- 剑指offer——python【第29题】最小的K个数
- C语言基础复习:字符,字符数组,字符串,字符指针
- Libevent源码分析—event_base_dispatch()
- day17-json格式转换
- 『Sklearn』框架自带数据集接口
- (转)MySQL登陆后提示符的修改
- 基于curl的异步http实现
- window7下 cmd命令行 Mysql导出表结构 + 表数据