ph-utils 0.1.1 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/dom.d.ts +6 -2
- package/lib/dom.js +28 -3
- package/package.json +1 -1
package/lib/dom.d.ts
CHANGED
@@ -32,9 +32,13 @@ 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,如果配置该项,则表明为委托事件;eventStop 表明是否在点击了事件节点时触发,默认为 false
|
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
|
+
eventStop?: boolean;
|
41
|
+
}): void;
|
38
42
|
/**
|
39
43
|
* 设置或获取节点的 innerHTML 属性
|
40
44
|
* @param element
|
package/lib/dom.js
CHANGED
@@ -54,10 +54,35 @@ 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 = { eventStop: false };
|
61
|
+
if (typeof once === 'boolean') {
|
62
|
+
eventExtra.once = once;
|
63
|
+
}
|
64
|
+
else {
|
65
|
+
eventExtra = once;
|
66
|
+
}
|
67
|
+
if (eventExtra.eventFlag != null) {
|
68
|
+
element.setAttribute(eventExtra.eventFlag, '__stop__');
|
69
|
+
element.addEventListener(listener, (e) => {
|
70
|
+
let target = e.target;
|
71
|
+
let flag = '';
|
72
|
+
do {
|
73
|
+
flag = target.getAttribute(eventExtra.eventFlag) || '';
|
74
|
+
if (flag === '') {
|
75
|
+
target = target.parentNode;
|
76
|
+
}
|
77
|
+
} while (flag === '');
|
78
|
+
if (flag !== '__stop__' || eventExtra.eventStop) {
|
79
|
+
fn(e, target, flag);
|
80
|
+
}
|
81
|
+
}, eventExtra);
|
82
|
+
}
|
83
|
+
else {
|
84
|
+
element.addEventListener(listener, fn, eventExtra);
|
85
|
+
}
|
61
86
|
}
|
62
87
|
/**
|
63
88
|
* 设置或获取节点的 innerHTML 属性
|