ph-utils 0.17.2 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/dom.d.ts +12 -0
- package/lib/dom.js +19 -0
- package/package.json +1 -1
package/lib/dom.d.ts
CHANGED
|
@@ -75,6 +75,18 @@ export declare function hasClass(elem: HTMLElement, clazz: string): boolean;
|
|
|
75
75
|
* @param clazz - 要切换的类名。
|
|
76
76
|
*/
|
|
77
77
|
export declare function toggleClass(el: HTMLElement, clazz: string): void;
|
|
78
|
+
/**
|
|
79
|
+
* 替换指定元素的 CSS 类
|
|
80
|
+
*
|
|
81
|
+
* 该函数用于根据给定的旧类名或类索引,将其替换为新类名。
|
|
82
|
+
* 如果旧类不存在,则不会进行替换。
|
|
83
|
+
*
|
|
84
|
+
* @param el - 目标 HTML 元素,将对其类进行操作
|
|
85
|
+
* @param oldClazz - 要替换的旧类名或类索引(基于数字时对应 classList 的索引位置)
|
|
86
|
+
* @param newClazz - 新的类名,用于替换旧的类
|
|
87
|
+
* @returns void
|
|
88
|
+
*/
|
|
89
|
+
export declare function replaceClass(el: HTMLElement, oldClazz: string | number, newClazz: string): void;
|
|
78
90
|
type EventHandler = EventListenerOrEventListenerObject | ((e: CustomEvent) => void);
|
|
79
91
|
/**
|
|
80
92
|
* 为节点添加事件处理
|
package/lib/dom.js
CHANGED
|
@@ -124,6 +124,25 @@ export function toggleClass(el, clazz) {
|
|
|
124
124
|
addClass(el, clazz);
|
|
125
125
|
}
|
|
126
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* 替换指定元素的 CSS 类
|
|
129
|
+
*
|
|
130
|
+
* 该函数用于根据给定的旧类名或类索引,将其替换为新类名。
|
|
131
|
+
* 如果旧类不存在,则不会进行替换。
|
|
132
|
+
*
|
|
133
|
+
* @param el - 目标 HTML 元素,将对其类进行操作
|
|
134
|
+
* @param oldClazz - 要替换的旧类名或类索引(基于数字时对应 classList 的索引位置)
|
|
135
|
+
* @param newClazz - 新的类名,用于替换旧的类
|
|
136
|
+
* @returns void
|
|
137
|
+
*/
|
|
138
|
+
export function replaceClass(el, oldClazz, newClazz) {
|
|
139
|
+
// 判断 oldClazz 是否为数字,若是则通过索引从 el.classList 中获取对应类名,否则直接使用字符串值
|
|
140
|
+
let old = typeof oldClazz === "number" ? el.classList.item(oldClazz) : oldClazz;
|
|
141
|
+
// 如果旧类存在,则使用 classList.replace 方法进行替换
|
|
142
|
+
if (old) {
|
|
143
|
+
el.classList.replace(old, newClazz);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
127
146
|
/**
|
|
128
147
|
* 为节点添加事件处理
|
|
129
148
|
* @param {HTMLElement} element 添加事件的节点
|