ph-utils 0.1.1 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/dom.d.ts +5 -2
- package/lib/dom.js +27 -3
- package/package.json +1 -1
package/lib/dom.d.ts
CHANGED
@@ -32,9 +32,12 @@ export declare function transform(element: HTMLElement, value: string): void;
|
|
32
32
|
* @param {HTMLElement} element 添加事件的节点
|
33
33
|
* @param {string} listener 事件名称
|
34
34
|
* @param {function} event 事件处理函数
|
35
|
-
* @param {boolean}
|
35
|
+
* @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
|
36
36
|
*/
|
37
|
-
export declare function on(element: HTMLElement, listener: string,
|
37
|
+
export declare function on(element: HTMLElement, listener: string, fn: (e: Event, target?: HTMLElement, flag?: string) => void, once?: boolean | {
|
38
|
+
once?: boolean;
|
39
|
+
eventFlag?: string;
|
40
|
+
}): void;
|
38
41
|
/**
|
39
42
|
* 设置或获取节点的 innerHTML 属性
|
40
43
|
* @param element
|
package/lib/dom.js
CHANGED
@@ -54,10 +54,34 @@ export function transform(element, value) {
|
|
54
54
|
* @param {HTMLElement} element 添加事件的节点
|
55
55
|
* @param {string} listener 事件名称
|
56
56
|
* @param {function} event 事件处理函数
|
57
|
-
* @param {boolean}
|
57
|
+
* @param {boolean} onceOrConfig 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
|
58
58
|
*/
|
59
|
-
export function on(element, listener,
|
60
|
-
|
59
|
+
export function on(element, listener, fn, once = false) {
|
60
|
+
let eventExtra = {};
|
61
|
+
if (typeof once === 'boolean') {
|
62
|
+
eventExtra.once = true;
|
63
|
+
}
|
64
|
+
else {
|
65
|
+
eventExtra = once;
|
66
|
+
}
|
67
|
+
if (eventExtra.eventFlag !== null) {
|
68
|
+
element.addEventListener(listener, (e) => {
|
69
|
+
let target = e.target;
|
70
|
+
let flag = '';
|
71
|
+
do {
|
72
|
+
flag = target.getAttribute(eventExtra.eventFlag) || '';
|
73
|
+
if (flag === '') {
|
74
|
+
target = target.parentNode;
|
75
|
+
}
|
76
|
+
} while (flag === '');
|
77
|
+
if (flag !== '__stop__') {
|
78
|
+
fn(e, target, flag);
|
79
|
+
}
|
80
|
+
}, eventExtra);
|
81
|
+
}
|
82
|
+
else {
|
83
|
+
element.addEventListener(listener, fn, eventExtra);
|
84
|
+
}
|
61
85
|
}
|
62
86
|
/**
|
63
87
|
* 设置或获取节点的 innerHTML 属性
|