@whitesev/domutils 1.6.7 → 1.7.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.
Files changed (55) hide show
  1. package/dist/index.amd.js +1955 -1067
  2. package/dist/index.amd.js.map +1 -1
  3. package/dist/index.amd.min.js +2 -0
  4. package/dist/index.amd.min.js.map +1 -0
  5. package/dist/index.cjs.js +1955 -1067
  6. package/dist/index.cjs.js.map +1 -1
  7. package/dist/index.cjs.min.js +2 -0
  8. package/dist/index.cjs.min.js.map +1 -0
  9. package/dist/index.esm.js +1955 -1067
  10. package/dist/index.esm.js.map +1 -1
  11. package/dist/index.esm.min.js +2 -0
  12. package/dist/index.esm.min.js.map +1 -0
  13. package/dist/index.iife.js +1955 -1067
  14. package/dist/index.iife.js.map +1 -1
  15. package/dist/index.iife.min.js +2 -0
  16. package/dist/index.iife.min.js.map +1 -0
  17. package/dist/index.system.js +1955 -1067
  18. package/dist/index.system.js.map +1 -1
  19. package/dist/index.system.min.js +2 -0
  20. package/dist/index.system.min.js.map +1 -0
  21. package/dist/index.umd.js +1955 -1067
  22. package/dist/index.umd.js.map +1 -1
  23. package/dist/index.umd.min.js +2 -0
  24. package/dist/index.umd.min.js.map +1 -0
  25. package/dist/types/index.d.ts +1 -1
  26. package/dist/types/src/{DOMUtilsCommonUtils.d.ts → CommonUtils.d.ts} +21 -9
  27. package/dist/types/src/ElementAnimate.d.ts +89 -0
  28. package/dist/types/src/{DOMUtilsEvent.d.ts → ElementEvent.d.ts} +19 -84
  29. package/dist/types/src/ElementHandler.d.ts +17 -0
  30. package/dist/types/src/ElementSelector.d.ts +96 -0
  31. package/dist/types/src/ElementWait.d.ts +278 -0
  32. package/dist/types/src/GlobalData.d.ts +4 -0
  33. package/dist/types/src/{DOMUtilsOriginPrototype.d.ts → OriginPrototype.d.ts} +1 -2
  34. package/dist/types/src/Utils.d.ts +68 -0
  35. package/dist/types/src/{DOMUtils.d.ts → index.d.ts} +157 -177
  36. package/dist/types/src/types/env.d.ts +9 -0
  37. package/dist/types/src/types/global.d.ts +0 -2
  38. package/dist/types/src/types/gm.d.ts +0 -4
  39. package/index.ts +1 -1
  40. package/package.json +6 -2
  41. package/src/{DOMUtilsCommonUtils.ts → CommonUtils.ts} +27 -13
  42. package/src/ElementAnimate.ts +290 -0
  43. package/src/{DOMUtilsEvent.ts → ElementEvent.ts} +188 -370
  44. package/src/ElementHandler.ts +43 -0
  45. package/src/ElementSelector.ts +260 -0
  46. package/src/ElementWait.ts +699 -0
  47. package/src/GlobalData.ts +5 -0
  48. package/src/{DOMUtilsOriginPrototype.ts → OriginPrototype.ts} +1 -3
  49. package/src/Utils.ts +386 -0
  50. package/src/{DOMUtils.ts → index.ts} +679 -758
  51. package/src/types/env.d.ts +9 -0
  52. package/src/types/global.d.ts +0 -2
  53. package/src/types/gm.d.ts +0 -4
  54. package/dist/types/src/DOMUtilsData.d.ts +0 -5
  55. package/src/DOMUtilsData.ts +0 -7
@@ -1,19 +1,32 @@
1
- import { DOMUtilsCommonUtils } from "./DOMUtilsCommonUtils";
2
- import { DOMUtilsEvent } from "./DOMUtilsEvent";
1
+ import { CommonUtils } from "./CommonUtils";
3
2
  import type { DOMUtilsCreateElementAttributesMap } from "./types/DOMUtilsEvent";
4
- import { type ParseHTMLReturnType, type DOMUtilsTargetElementType } from "./types/global";
3
+ import { type DOMUtilsTargetElementType } from "./types/global";
5
4
  import type { WindowApiOption } from "./types/WindowApi";
6
- import { version } from "./../package.json";
5
+ import { version } from "../package.json";
6
+ import { ElementHandler } from "./ElementHandler";
7
7
 
8
- class DOMUtils extends DOMUtilsEvent {
8
+ class DOMUtils extends ElementHandler {
9
9
  constructor(option?: WindowApiOption) {
10
10
  super(option);
11
11
  }
12
12
  /** 版本号 */
13
13
  version = version;
14
+ /**
15
+ * 取消挂载在window下的DOMUtils并返回DOMUtils
16
+ * @example
17
+ * let DOMUtils = window.DOMUtils.noConflict()
18
+ */
19
+ noConflict() {
20
+ const that = this;
21
+ if (that.windowApi.window.DOMUtils) {
22
+ CommonUtils.delete(window, "DOMUtils");
23
+ }
24
+ that.windowApi.window.DOMUtils = this;
25
+ return this;
26
+ }
14
27
  /**
15
28
  * 获取元素的属性值
16
- * @param element 目标元素
29
+ * @param $el 目标元素
17
30
  * @param attrName 属性名
18
31
  * @example
19
32
  * // 获取a.xx元素的href属性
@@ -21,10 +34,10 @@ class DOMUtils extends DOMUtilsEvent {
21
34
  * DOMUtils.attr("a.xx","href");
22
35
  * > https://xxxx....
23
36
  */
24
- attr(element: DOMUtilsTargetElementType | Element, attrName: string): string;
37
+ attr($el: DOMUtilsTargetElementType | Element, attrName: string): string;
25
38
  /**
26
39
  * 设置元素的属性值
27
- * @param element 目标元素
40
+ * @param $el 目标元素
28
41
  * @param attrName 属性名
29
42
  * @param attrValue 属性值
30
43
  * @example
@@ -32,31 +45,31 @@ class DOMUtils extends DOMUtilsEvent {
32
45
  * DOMUtils.attr(document.querySelector("a.xx"),"href","abcd");
33
46
  * DOMUtils.attr("a.xx","href","abcd");
34
47
  */
35
- attr(element: DOMUtilsTargetElementType | Element, attrName: string, attrValue: string | boolean | number): void;
36
- attr(element: DOMUtilsTargetElementType | Element, attrName: string, attrValue?: any) {
37
- const DOMUtilsContext = this;
38
- if (typeof element === "string") {
39
- element = DOMUtilsContext.selectorAll(element);
48
+ attr($el: DOMUtilsTargetElementType | Element, attrName: string, attrValue: string | boolean | number): void;
49
+ attr($el: DOMUtilsTargetElementType | Element, attrName: string, attrValue?: any) {
50
+ const that = this;
51
+ if (typeof $el === "string") {
52
+ $el = that.selectorAll($el);
40
53
  }
41
- if (element == null) {
54
+ if ($el == null) {
42
55
  return;
43
56
  }
44
- if (DOMUtilsCommonUtils.isNodeList(element)) {
57
+ if (CommonUtils.isNodeList($el)) {
45
58
  if (attrValue == null) {
46
59
  // 获取属性
47
- return DOMUtilsContext.attr(element[0] as HTMLElement, attrName, attrValue);
60
+ return that.attr($el[0] as HTMLElement, attrName, attrValue);
48
61
  } else {
49
62
  // 设置属性
50
- element.forEach(($ele) => {
51
- DOMUtilsContext.attr($ele as HTMLElement, attrName, attrValue);
63
+ $el.forEach(($elItem) => {
64
+ that.attr($elItem as HTMLElement, attrName, attrValue);
52
65
  });
53
66
  return;
54
67
  }
55
68
  }
56
69
  if (attrValue == null) {
57
- return element.getAttribute(attrName);
70
+ return $el.getAttribute(attrName);
58
71
  } else {
59
- element.setAttribute(attrName, attrValue);
72
+ $el.setAttribute(attrName, attrValue);
60
73
  }
61
74
  }
62
75
  /**
@@ -137,11 +150,11 @@ class DOMUtils extends DOMUtilsEvent {
137
150
  /** 自定义属性 */
138
151
  attributes?: DOMUtilsCreateElementAttributesMap
139
152
  ): HTMLElementTagNameMap[K] {
140
- const DOMUtilsContext = this;
141
- const tempElement = DOMUtilsContext.windowApi.document.createElement(tagName);
153
+ const that = this;
154
+ const $el = that.windowApi.document.createElement(tagName);
142
155
  if (typeof property === "string") {
143
- DOMUtilsContext.html(tempElement, property);
144
- return tempElement;
156
+ that.html($el, property);
157
+ return $el;
145
158
  }
146
159
  if (property == null) {
147
160
  property = {};
@@ -152,10 +165,10 @@ class DOMUtils extends DOMUtilsEvent {
152
165
  Object.keys(property).forEach((key) => {
153
166
  const value = property[key];
154
167
  if (key === "innerHTML") {
155
- DOMUtilsContext.html(tempElement, value);
168
+ that.html($el, value);
156
169
  return;
157
170
  }
158
- (tempElement as any)[key] = value;
171
+ (<any>$el)[key] = value;
159
172
  });
160
173
  Object.keys(attributes).forEach((key) => {
161
174
  let value = attributes[key];
@@ -166,13 +179,13 @@ class DOMUtils extends DOMUtilsEvent {
166
179
  /* function转字符串 */
167
180
  value = value.toString();
168
181
  }
169
- tempElement.setAttribute(key, value);
182
+ $el.setAttribute(key, value);
170
183
  });
171
- return tempElement;
184
+ return $el;
172
185
  }
173
186
  /**
174
187
  * 获取元素的样式属性值
175
- * @param element 目标元素
188
+ * @param $el 目标元素
176
189
  * @param property 样式属性名或包含多个属性名和属性值的对象
177
190
  * @example
178
191
  * // 获取元素a.xx的CSS属性display
@@ -180,10 +193,10 @@ class DOMUtils extends DOMUtilsEvent {
180
193
  * DOMUtils.css("a.xx","display");
181
194
  * > "none"
182
195
  * */
183
- css(element: DOMUtilsTargetElementType, property: keyof Omit<CSSStyleDeclaration, "zIndex"> | "z-index"): string;
196
+ css($el: DOMUtilsTargetElementType, property: keyof Omit<CSSStyleDeclaration, "zIndex"> | "z-index"): string;
184
197
  /**
185
198
  * 获取元素的样式属性值
186
- * @param element 目标元素
199
+ * @param $el 目标元素
187
200
  * @param property 样式属性名或包含多个属性名和属性值的对象
188
201
  * @example
189
202
  * // 获取元素a.xx的CSS属性display
@@ -191,10 +204,10 @@ class DOMUtils extends DOMUtilsEvent {
191
204
  * DOMUtils.css("a.xx","display");
192
205
  * > "none"
193
206
  * */
194
- css(element: DOMUtilsTargetElementType, property: string): string;
207
+ css($el: DOMUtilsTargetElementType, property: string): string;
195
208
  /**
196
209
  * 设置元素的样式属性
197
- * @param element 目标元素
210
+ * @param $el 目标元素
198
211
  * @param property 样式属性名或包含多个属性名和属性值的对象
199
212
  * @param value 样式属性值
200
213
  * @example
@@ -209,13 +222,13 @@ class DOMUtils extends DOMUtilsEvent {
209
222
  * DOMUtils.css(document.querySelector("a.xx"),"top",10);
210
223
  * */
211
224
  css(
212
- element: DOMUtilsTargetElementType,
225
+ $el: DOMUtilsTargetElementType,
213
226
  property: (keyof Omit<CSSStyleDeclaration, "zIndex"> | "z-index") & string,
214
227
  value: string | number
215
228
  ): string;
216
229
  /**
217
230
  * 设置元素的样式属性
218
- * @param element 目标元素
231
+ * @param $el 目标元素
219
232
  * @param property 样式属性名或包含多个属性名和属性值的对象
220
233
  * @param value 样式属性值
221
234
  * @example
@@ -228,7 +241,7 @@ class DOMUtils extends DOMUtilsEvent {
228
241
  * DOMUtils.css(document.querySelector("a.xx"),{ top: 10 });
229
242
  * */
230
243
  css(
231
- element: DOMUtilsTargetElementType,
244
+ $el: DOMUtilsTargetElementType,
232
245
  property:
233
246
  | {
234
247
  [P in keyof Omit<CSSStyleDeclaration, "zIndex">]?: CSSStyleDeclaration[P];
@@ -241,7 +254,7 @@ class DOMUtils extends DOMUtilsEvent {
241
254
  }
242
255
  ): string;
243
256
  css(
244
- element: DOMUtilsTargetElementType,
257
+ $el: DOMUtilsTargetElementType,
245
258
  property:
246
259
  | keyof Omit<CSSStyleDeclaration, "zIndex">
247
260
  | string
@@ -250,7 +263,7 @@ class DOMUtils extends DOMUtilsEvent {
250
263
  },
251
264
  value?: string | number
252
265
  ) {
253
- const DOMUtilsContext = this;
266
+ const that = this;
254
267
  /**
255
268
  * 把纯数字没有px的加上
256
269
  */
@@ -264,28 +277,28 @@ class DOMUtils extends DOMUtilsEvent {
264
277
  }
265
278
  return propertyValue;
266
279
  }
267
- if (typeof element === "string") {
268
- element = DOMUtilsContext.selectorAll(element);
280
+ if (typeof $el === "string") {
281
+ $el = that.selectorAll($el);
269
282
  }
270
- if (element == null) {
283
+ if ($el == null) {
271
284
  return;
272
285
  }
273
- if (DOMUtilsCommonUtils.isNodeList(element)) {
286
+ if (CommonUtils.isNodeList($el)) {
274
287
  if (typeof property === "string") {
275
288
  if (value == null) {
276
289
  // 获取属性
277
- return DOMUtilsContext.css(element[0] as HTMLElement, property);
290
+ return that.css($el[0] as HTMLElement, property);
278
291
  } else {
279
292
  // 设置属性
280
- element.forEach(($ele) => {
281
- DOMUtilsContext.css($ele as HTMLElement, property);
293
+ $el.forEach(($elItem) => {
294
+ that.css($elItem as HTMLElement, property);
282
295
  });
283
296
  return;
284
297
  }
285
298
  } else if (typeof property === "object") {
286
299
  // 设置属性
287
- element.forEach(($ele) => {
288
- DOMUtilsContext.css($ele as HTMLElement, property as object);
300
+ $el.forEach(($elItem) => {
301
+ that.css($elItem as HTMLElement, property as object);
289
302
  });
290
303
  return;
291
304
  }
@@ -297,15 +310,15 @@ class DOMUtils extends DOMUtilsEvent {
297
310
  .trim()
298
311
  .replace(/!important$/gi, "")
299
312
  .trim();
300
- element.style.setProperty(propertyName, propertyValue, "important");
313
+ $el.style.setProperty(propertyName, propertyValue, "important");
301
314
  } else {
302
315
  propertyValue = handlePixe(propertyName, propertyValue);
303
- element.style.setProperty(propertyName, propertyValue);
316
+ $el.style.setProperty(propertyName, propertyValue);
304
317
  }
305
318
  };
306
319
  if (typeof property === "string") {
307
320
  if (value == null) {
308
- return DOMUtilsContext.windowApi.globalThis.getComputedStyle(element).getPropertyValue(property);
321
+ return that.windowApi.globalThis.getComputedStyle($el).getPropertyValue(property);
309
322
  } else {
310
323
  setStyleProperty(property, value);
311
324
  }
@@ -321,7 +334,7 @@ class DOMUtils extends DOMUtilsEvent {
321
334
  }
322
335
  /**
323
336
  * 获取元素的文本内容,优先返回textContent
324
- * @param element 目标元素
337
+ * @param $el 目标元素
325
338
  * @returns 如果传入了text,则返回undefined;否则返回文本内容
326
339
  * @example
327
340
  * // 设置元素a.xx的文本内容为abcd
@@ -329,10 +342,10 @@ class DOMUtils extends DOMUtilsEvent {
329
342
  * DOMUtils.text("a.xx","abcd")
330
343
  * DOMUtils.text("a.xx",document.querySelector("b"))
331
344
  * */
332
- text(element: DOMUtilsTargetElementType | Element | DocumentFragment | Node): string;
345
+ text($el: DOMUtilsTargetElementType | Element | DocumentFragment | Node): string;
333
346
  /**
334
347
  * 设置元素的文本内容
335
- * @param element 目标元素
348
+ * @param $el 目标元素
336
349
  * @param text (可选)文本内容
337
350
  * @returns 如果传入了text,则返回undefined;否则返回文本内容
338
351
  * @example
@@ -342,43 +355,42 @@ class DOMUtils extends DOMUtilsEvent {
342
355
  * DOMUtils.text("a.xx",document.querySelector("b"))
343
356
  * */
344
357
  text(
345
- element: DOMUtilsTargetElementType | Element | DocumentFragment | Node,
358
+ $el: DOMUtilsTargetElementType | Element | DocumentFragment | Node,
346
359
  text: string | HTMLElement | Element | number
347
360
  ): void;
348
- text(element: DOMUtilsTargetElementType | Element | DocumentFragment | Node, text?: any) {
349
- const DOMUtilsContext = this;
350
- if (typeof element === "string") {
351
- element = DOMUtilsContext.selectorAll(element);
361
+ text($el: DOMUtilsTargetElementType | Element | DocumentFragment | Node, text?: any) {
362
+ const that = this;
363
+ if (typeof $el === "string") {
364
+ $el = that.selectorAll($el);
352
365
  }
353
- if (element == null) {
366
+ if ($el == null) {
354
367
  return;
355
368
  }
356
- if (DOMUtilsCommonUtils.isNodeList(element)) {
369
+ if (CommonUtils.isNodeList($el)) {
357
370
  if (text == null) {
358
371
  // 获取
359
- return DOMUtilsContext.text(element[0] as HTMLElement);
372
+ return that.text($el[0] as HTMLElement);
360
373
  } else {
361
374
  // 设置
362
- element.forEach(($ele) => {
363
- DOMUtilsContext.text($ele as HTMLElement, text);
375
+ $el.forEach(($elItem) => {
376
+ that.text($elItem as HTMLElement, text);
364
377
  });
365
378
  }
366
379
  return;
367
380
  }
368
381
  if (text == null) {
369
- return element.textContent || (<HTMLElement>element).innerText;
382
+ return $el.textContent || (<HTMLElement>$el).innerText;
370
383
  } else {
371
384
  if (text instanceof Node) {
372
385
  text = text.textContent || (text as HTMLElement).innerText;
373
386
  }
374
- if ("textContent" in element) {
375
- element.textContent = text as string;
376
- } else if ("innerText" in element) {
377
- (element as HTMLElement).innerText = text as string;
387
+ if ("textContent" in $el) {
388
+ $el.textContent = text as string;
389
+ } else if ("innerText" in $el) {
390
+ ($el as HTMLElement).innerText = text as string;
378
391
  }
379
392
  }
380
393
  }
381
-
382
394
  /**
383
395
  * 设置元素的HTML内容
384
396
  * @param element 目标元素
@@ -393,7 +405,7 @@ class DOMUtils extends DOMUtilsEvent {
393
405
  html(element: DOMUtilsTargetElementType, html: string | HTMLElement | Element | number): void;
394
406
  /**
395
407
  * 获取元素的HTML内容
396
- * @param element 目标元素
408
+ * @param $el 目标元素
397
409
  * @param html (可选)HTML内容|元素
398
410
  * @returns 如果传入了html,则返回undefined;否则返回HTML内容
399
411
  * @example
@@ -402,37 +414,37 @@ class DOMUtils extends DOMUtilsEvent {
402
414
  * DOMUtils.html("a.xx","<b>abcd</b>")
403
415
  * DOMUtils.html("a.xx",document.querySelector("b"))
404
416
  * */
405
- html(element: DOMUtilsTargetElementType): string;
406
- html(element: DOMUtilsTargetElementType, html?: any) {
407
- const DOMUtilsContext = this;
408
- if (typeof element === "string") {
409
- element = DOMUtilsContext.selectorAll(element);
417
+ html($el: DOMUtilsTargetElementType): string;
418
+ html($el: DOMUtilsTargetElementType, html?: any) {
419
+ const that = this;
420
+ if (typeof $el === "string") {
421
+ $el = that.selectorAll($el);
410
422
  }
411
- if (element == null) {
423
+ if ($el == null) {
412
424
  return;
413
425
  }
414
- if (DOMUtilsCommonUtils.isNodeList(element)) {
426
+ if (CommonUtils.isNodeList($el)) {
415
427
  if (html == null) {
416
428
  // 获取
417
- return DOMUtilsContext.html(element[0] as HTMLElement);
429
+ return that.html($el[0] as HTMLElement);
418
430
  } else {
419
431
  // 设置
420
- element.forEach(($ele) => {
421
- DOMUtilsContext.html($ele as HTMLElement, html);
432
+ $el.forEach(($elItem) => {
433
+ that.html($elItem as HTMLElement, html);
422
434
  });
423
435
  }
424
436
  return;
425
437
  }
426
438
  if (html == null) {
427
439
  // 获取
428
- return element.innerHTML;
440
+ return $el.innerHTML;
429
441
  } else {
430
442
  // 设置
431
443
  if (html instanceof Element) {
432
444
  html = html.innerHTML;
433
445
  }
434
- if ("innerHTML" in element) {
435
- DOMUtilsCommonUtils.setSafeHTML(element, html);
446
+ if ("innerHTML" in $el) {
447
+ CommonUtils.setSafeHTML($el, html);
436
448
  }
437
449
  }
438
450
  }
@@ -440,23 +452,23 @@ class DOMUtils extends DOMUtilsEvent {
440
452
  * 获取移动元素的transform偏移
441
453
  */
442
454
  getTransform(
443
- element: HTMLElement,
455
+ $el: HTMLElement,
444
456
  isShow: boolean = false
445
457
  ): {
446
458
  transformLeft: number;
447
459
  transformTop: number;
448
460
  } {
449
- const DOMUtilsContext = this;
461
+ const that = this;
450
462
  let transform_left = 0;
451
463
  let transform_top = 0;
452
- if (!(isShow || (!isShow && DOMUtilsCommonUtils.isShow(element)))) {
464
+ if (!(isShow || (!isShow && CommonUtils.isShow($el)))) {
453
465
  /* 未显示 */
454
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
455
- const transformInfo = DOMUtilsContext.getTransform(element, true);
466
+ const { recovery } = CommonUtils.forceShow($el);
467
+ const transformInfo = that.getTransform($el, true);
456
468
  recovery();
457
469
  return transformInfo;
458
470
  }
459
- const elementTransform = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element).transform;
471
+ const elementTransform = that.windowApi.globalThis.getComputedStyle($el).transform;
460
472
  if (elementTransform != null && elementTransform !== "none" && elementTransform !== "") {
461
473
  const elementTransformSplit = elementTransform.match(/\((.+)\)/)?.[1].split(",");
462
474
  if (elementTransformSplit) {
@@ -475,7 +487,7 @@ class DOMUtils extends DOMUtilsEvent {
475
487
 
476
488
  /**
477
489
  * 设置元素的value属性值
478
- * @param element 目标元素
490
+ * @param $el 目标元素
479
491
  * @param value (可选)value属性值
480
492
  * @returns 如果传入了value,则返回undefined;否则返回value属性值
481
493
  * > true
@@ -485,7 +497,7 @@ class DOMUtils extends DOMUtilsEvent {
485
497
  * DOMUtils.val("input.xx",true)
486
498
  * */
487
499
  val(
488
- element:
500
+ $el:
489
501
  | HTMLInputElement
490
502
  | HTMLTextAreaElement
491
503
  | HTMLSelectElement
@@ -496,13 +508,13 @@ class DOMUtils extends DOMUtilsEvent {
496
508
  ): void;
497
509
  /**
498
510
  * 获取value属性值
499
- * @param element 目标元素
511
+ * @param $el 目标元素
500
512
  * @example
501
513
  * // 获取元素textarea的值
502
514
  * DOMUtils.val(document.querySelector("textarea.xx"))
503
515
  * */
504
516
  val(
505
- element:
517
+ $el:
506
518
  | HTMLInputElement
507
519
  | HTMLTextAreaElement
508
520
  | HTMLSelectElement
@@ -512,14 +524,14 @@ class DOMUtils extends DOMUtilsEvent {
512
524
  ): string;
513
525
  /**
514
526
  * 获取value属性值
515
- * @param element 目标元素
527
+ * @param $el 目标元素
516
528
  * @example
517
529
  * // 获取元素input.xx的复选框值
518
530
  * DOMUtils.val(document.querySelector("input.xx"))
519
531
  * DOMUtils.val("input.xx")
520
532
  * */
521
533
  val(
522
- element:
534
+ $el:
523
535
  | HTMLInputElement
524
536
  | HTMLTextAreaElement
525
537
  | HTMLSelectElement
@@ -527,7 +539,7 @@ class DOMUtils extends DOMUtilsEvent {
527
539
  | NodeListOf<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>
528
540
  ): boolean | string;
529
541
  val(
530
- element:
542
+ $el:
531
543
  | HTMLInputElement
532
544
  | HTMLTextAreaElement
533
545
  | HTMLSelectElement
@@ -536,45 +548,44 @@ class DOMUtils extends DOMUtilsEvent {
536
548
  | NodeListOf<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>,
537
549
  value?: string | boolean | number
538
550
  ) {
539
- const DOMUtilsContext = this;
540
- if (typeof element === "string") {
541
- element = DOMUtilsContext.selectorAll(element);
551
+ const that = this;
552
+ if (typeof $el === "string") {
553
+ $el = that.selectorAll($el);
542
554
  }
543
- if (element == null) {
555
+ if ($el == null) {
544
556
  return;
545
557
  }
546
- if (DOMUtilsCommonUtils.isNodeList(element)) {
558
+ if (CommonUtils.isNodeList($el)) {
547
559
  if (value == null) {
548
560
  // 获取
549
- return DOMUtilsContext.val(element[0] as HTMLInputElement);
561
+ return that.val($el[0] as HTMLInputElement);
550
562
  } else {
551
563
  // 设置
552
- element.forEach(($ele) => {
553
- DOMUtilsContext.val($ele as HTMLInputElement, value);
564
+ $el.forEach(($elItem) => {
565
+ that.val($elItem as HTMLInputElement, value);
554
566
  });
555
567
  }
556
568
  return;
557
569
  }
558
570
  if (value == null) {
559
571
  // 获取
560
- if (element.localName === "input" && (element.type === "checkbox" || element.type === "radio")) {
561
- return (element as HTMLInputElement).checked;
572
+ if ($el.localName === "input" && ($el.type === "checkbox" || $el.type === "radio")) {
573
+ return ($el as HTMLInputElement).checked;
562
574
  } else {
563
- return element.value;
575
+ return $el.value;
564
576
  }
565
577
  } else {
566
578
  // 设置
567
- if (element.localName === "input" && (element.type === "checkbox" || element.type === "radio")) {
568
- (element as HTMLInputElement).checked = !!value;
579
+ if ($el.localName === "input" && ($el.type === "checkbox" || $el.type === "radio")) {
580
+ ($el as HTMLInputElement).checked = !!value;
569
581
  } else {
570
- element.value = value as string;
582
+ $el.value = value as string;
571
583
  }
572
584
  }
573
585
  }
574
-
575
586
  /**
576
587
  * 获取元素的属性值
577
- * @param element 目标元素
588
+ * @param $el 目标元素
578
589
  * @param propName 属性名
579
590
  * @param propValue 属性值
580
591
  * @example
@@ -583,10 +594,10 @@ class DOMUtils extends DOMUtilsEvent {
583
594
  * DOMUtils.val("a.xx","data-value")
584
595
  * > undefined
585
596
  * */
586
- prop<T>(element: DOMUtilsTargetElementType | DocumentFragment, propName: string): T;
597
+ prop<T>($el: DOMUtilsTargetElementType | DocumentFragment, propName: string): T;
587
598
  /**
588
599
  * 设置元素的属性值
589
- * @param element 目标元素
600
+ * @param $el 目标元素
590
601
  * @param propName 属性名
591
602
  * @param propValue 属性值
592
603
  * @example
@@ -594,181 +605,146 @@ class DOMUtils extends DOMUtilsEvent {
594
605
  * DOMUtils.val(document.querySelector("a.xx"),"data-value",1)
595
606
  * DOMUtils.val("a.xx","data-value",1)
596
607
  * */
597
- prop<T>(element: DOMUtilsTargetElementType | DocumentFragment, propName: string, propValue: T): void;
598
- prop(element: DOMUtilsTargetElementType | DocumentFragment, propName: string, propValue?: any) {
599
- const DOMUtilsContext = this;
600
- if (typeof element === "string") {
601
- element = DOMUtilsContext.selectorAll(element);
608
+ prop<T>($el: DOMUtilsTargetElementType | DocumentFragment, propName: string, propValue: T): void;
609
+ prop($el: DOMUtilsTargetElementType | DocumentFragment, propName: string, propValue?: any) {
610
+ const that = this;
611
+ if (typeof $el === "string") {
612
+ $el = that.selectorAll($el);
602
613
  }
603
- if (element == null) {
614
+ if ($el == null) {
604
615
  return;
605
616
  }
606
- if (DOMUtilsCommonUtils.isNodeList(element)) {
617
+ if (CommonUtils.isNodeList($el)) {
607
618
  if (propValue == null) {
608
619
  // 获取
609
- return DOMUtilsContext.prop(element[0] as HTMLElement, propName);
620
+ return that.prop($el[0] as HTMLElement, propName);
610
621
  } else {
611
622
  // 设置
612
- element.forEach(($ele) => {
613
- DOMUtilsContext.prop($ele as HTMLElement, propName, propValue);
623
+ $el.forEach(($elItem) => {
624
+ that.prop($elItem as HTMLElement, propName, propValue);
614
625
  });
615
626
  }
616
627
  return;
617
628
  }
618
629
  if (propValue == null) {
619
- return Reflect.get(element, propName);
630
+ return Reflect.get($el, propName);
620
631
  } else {
621
- if (element instanceof Element && propName === "innerHTML") {
622
- DOMUtilsContext.html(element, propValue);
632
+ if ($el instanceof Element && propName === "innerHTML") {
633
+ that.html($el, propValue);
623
634
  } else {
624
- Reflect.set(element, propName, propValue);
635
+ Reflect.set($el, propName, propValue);
625
636
  }
626
637
  }
627
638
  }
628
639
  /**
629
640
  * 移除元素的属性
630
- * @param element 目标元素
641
+ * @param $el 目标元素
631
642
  * @param attrName 属性名
632
643
  * @example
633
644
  * // 移除元素a.xx的属性data-value
634
645
  * DOMUtils.removeAttr(document.querySelector("a.xx"),"data-value")
635
646
  * DOMUtils.removeAttr("a.xx","data-value")
636
647
  * */
637
- removeAttr(element: DOMUtilsTargetElementType | Element, attrName: string) {
638
- const DOMUtilsContext = this;
639
- if (typeof element === "string") {
640
- element = DOMUtilsContext.selectorAll(element);
648
+ removeAttr($el: DOMUtilsTargetElementType | Element, attrName: string) {
649
+ const that = this;
650
+ if (typeof $el === "string") {
651
+ $el = that.selectorAll($el);
641
652
  }
642
- if (element == null) {
653
+ if ($el == null) {
643
654
  return;
644
655
  }
645
- if (DOMUtilsCommonUtils.isNodeList(element)) {
656
+ if (CommonUtils.isNodeList($el)) {
646
657
  // 设置
647
- element.forEach(($ele) => {
648
- DOMUtilsContext.removeAttr($ele as HTMLElement, attrName);
658
+ $el.forEach(($elItem) => {
659
+ that.removeAttr($elItem as HTMLElement, attrName);
649
660
  });
650
661
  return;
651
662
  }
652
- element.removeAttribute(attrName);
663
+ $el.removeAttribute(attrName);
653
664
  }
654
665
  /**
655
666
  * 移除元素class名
656
- * @param element 目标元素
667
+ * @param $el 目标元素
657
668
  * @param className 类名
658
669
  * @example
659
670
  * // 移除元素a.xx的className为xx
660
671
  * DOMUtils.removeClass(document.querySelector("a.xx"),"xx")
661
672
  * DOMUtils.removeClass("a.xx","xx")
662
673
  */
663
- removeClass(element: DOMUtilsTargetElementType | Element, className?: string | string[] | undefined | null) {
664
- const DOMUtilsContext = this;
665
- if (typeof element === "string") {
666
- element = DOMUtilsContext.selectorAll(element);
674
+ removeClass($el: DOMUtilsTargetElementType | Element, className?: string | string[] | undefined | null) {
675
+ const that = this;
676
+ if (typeof $el === "string") {
677
+ $el = that.selectorAll($el);
667
678
  }
668
- if (element == null) {
679
+ if ($el == null) {
669
680
  return;
670
681
  }
671
- if (DOMUtilsCommonUtils.isNodeList(element)) {
682
+ if (CommonUtils.isNodeList($el)) {
672
683
  // 设置
673
- element.forEach(($ele) => {
674
- DOMUtilsContext.removeClass($ele as HTMLElement, className);
684
+ $el.forEach(($elItem) => {
685
+ that.removeClass($elItem as HTMLElement, className);
675
686
  });
676
687
  return;
677
688
  }
678
689
  if (className == null) {
679
690
  // 清空全部className
680
- element.className = "";
691
+ $el.className = "";
681
692
  } else {
682
693
  if (!Array.isArray(className)) {
683
694
  className = className.trim().split(" ");
684
695
  }
685
696
  className.forEach((itemClassName) => {
686
- element.classList.remove(itemClassName);
697
+ $el.classList.remove(itemClassName);
687
698
  });
688
699
  }
689
700
  }
690
701
  /**
691
702
  * 移除元素的属性
692
- * @param element 目标元素
703
+ * @param $el 目标元素
693
704
  * @param propName 属性名
694
705
  * @example
695
706
  * // 移除元素a.xx的href属性
696
707
  * DOMUtils.removeProp(document.querySelector("a.xx"),"href")
697
708
  * DOMUtils.removeProp("a.xx","href")
698
709
  * */
699
- removeProp(element: DOMUtilsTargetElementType | DocumentFragment, propName: string) {
700
- const DOMUtilsContext = this;
701
- if (typeof element === "string") {
702
- element = DOMUtilsContext.selectorAll(element);
703
- }
704
- if (element == null) {
705
- return;
706
- }
707
- if (DOMUtilsCommonUtils.isNodeList(element)) {
708
- // 设置
709
- element.forEach(($ele) => {
710
- DOMUtilsContext.removeProp($ele as HTMLElement, propName);
711
- });
712
- return;
713
- }
714
- DOMUtilsCommonUtils.delete(element, propName);
715
- }
716
- /**
717
- * 将一个元素替换为另一个元素
718
- * @param element 目标元素
719
- * @param newElement 新元素
720
- * @example
721
- * // 替换元素a.xx为b.xx
722
- * DOMUtils.replaceWith(document.querySelector("a.xx"),document.querySelector("b.xx"))
723
- * DOMUtils.replaceWith("a.xx",'<b class="xx"></b>')
724
- */
725
- replaceWith(element: DOMUtilsTargetElementType, newElement: HTMLElement | string | Node) {
726
- const DOMUtilsContext = this;
727
- if (typeof element === "string") {
728
- element = DOMUtilsContext.selectorAll(element);
710
+ removeProp($el: DOMUtilsTargetElementType | DocumentFragment, propName: string) {
711
+ const that = this;
712
+ if (typeof $el === "string") {
713
+ $el = that.selectorAll($el);
729
714
  }
730
- if (element == null) {
715
+ if ($el == null) {
731
716
  return;
732
717
  }
733
- if (DOMUtilsCommonUtils.isNodeList(element)) {
718
+ if (CommonUtils.isNodeList($el)) {
734
719
  // 设置
735
- element.forEach(($ele) => {
736
- DOMUtilsContext.replaceWith($ele as HTMLElement, newElement);
720
+ $el.forEach(($elItem) => {
721
+ that.removeProp($elItem as HTMLElement, propName);
737
722
  });
738
723
  return;
739
724
  }
740
- if (typeof newElement === "string") {
741
- newElement = DOMUtilsContext.parseHTML(newElement, false, false);
742
- }
743
- const $parent = element.parentElement;
744
- if ($parent) {
745
- $parent.replaceChild(newElement as Node, element);
746
- } else {
747
- DOMUtilsContext.after(element, newElement as HTMLElement);
748
- element.remove();
749
- }
725
+ CommonUtils.delete($el, propName);
750
726
  }
751
727
  /**
752
728
  * 给元素添加class
753
- * @param element 目标元素
729
+ * @param $el 目标元素
754
730
  * @param className class名
755
731
  * @example
756
732
  * // 元素a.xx的className添加_vue_
757
733
  * DOMUtils.addClass(document.querySelector("a.xx"),"_vue_")
758
734
  * DOMUtils.addClass("a.xx","_vue_")
759
735
  * */
760
- addClass(element: DOMUtilsTargetElementType | Element, className: string | string[]) {
761
- const DOMUtilsContext = this;
762
- if (typeof element === "string") {
763
- element = DOMUtilsContext.selectorAll(element);
736
+ addClass($el: DOMUtilsTargetElementType | Element, className: string | string[]) {
737
+ const that = this;
738
+ if (typeof $el === "string") {
739
+ $el = that.selectorAll($el);
764
740
  }
765
- if (element == null) {
741
+ if ($el == null) {
766
742
  return;
767
743
  }
768
- if (DOMUtilsCommonUtils.isNodeList(element)) {
744
+ if (CommonUtils.isNodeList($el)) {
769
745
  // 设置
770
- element.forEach(($ele) => {
771
- DOMUtilsContext.addClass($ele as HTMLElement, className);
746
+ $el.forEach(($elItem) => {
747
+ that.addClass($elItem as HTMLElement, className);
772
748
  });
773
749
  return;
774
750
  }
@@ -779,31 +755,31 @@ class DOMUtils extends DOMUtilsEvent {
779
755
  if (itemClassName.trim() == "") {
780
756
  return;
781
757
  }
782
- element.classList.add(itemClassName);
758
+ $el.classList.add(itemClassName);
783
759
  });
784
760
  }
785
761
  /**
786
762
  * 判断元素是否存在className
787
- * @param element
763
+ * @param $el
788
764
  * @param className
789
765
  */
790
- hasClass(element: DOMUtilsTargetElementType | Element, className: string | string[]): boolean {
791
- const DOMUtilsContext = this;
792
- if (typeof element === "string") {
793
- element = DOMUtilsContext.selectorAll(element);
766
+ hasClass($el: DOMUtilsTargetElementType | Element, className: string | string[]): boolean {
767
+ const that = this;
768
+ if (typeof $el === "string") {
769
+ $el = that.selectorAll($el);
794
770
  }
795
- if (element == null) {
771
+ if ($el == null) {
796
772
  return false;
797
773
  }
798
- if (DOMUtilsCommonUtils.isNodeList(element)) {
774
+ if (CommonUtils.isNodeList($el)) {
799
775
  let flag = true;
800
- for (let index = 0; index < element.length; index++) {
801
- const $ele = element[index] as HTMLElement;
802
- flag = flag && DOMUtilsContext.hasClass($ele, className);
776
+ for (let index = 0; index < $el.length; index++) {
777
+ const $elItem = $el[index] as HTMLElement;
778
+ flag = flag && that.hasClass($elItem, className);
803
779
  }
804
780
  return flag;
805
781
  }
806
- if (!element?.classList) {
782
+ if (!$el?.classList) {
807
783
  return false;
808
784
  }
809
785
  if (!Array.isArray(className)) {
@@ -811,7 +787,7 @@ class DOMUtils extends DOMUtilsEvent {
811
787
  }
812
788
  for (let index = 0; index < className.length; index++) {
813
789
  const item = className[index].trim();
814
- if (!element.classList.contains(item)) {
790
+ if (!$el.classList.contains(item)) {
815
791
  return false;
816
792
  }
817
793
  }
@@ -819,7 +795,7 @@ class DOMUtils extends DOMUtilsEvent {
819
795
  }
820
796
  /**
821
797
  * 函数在元素内部末尾添加子元素或HTML字符串
822
- * @param element 目标元素
798
+ * @param $el 目标元素
823
799
  * @param content 子元素或HTML字符串
824
800
  * @example
825
801
  * // 元素a.xx的内部末尾添加一个元素
@@ -827,21 +803,21 @@ class DOMUtils extends DOMUtilsEvent {
827
803
  * DOMUtils.append("a.xx","'<b class="xx"></b>")
828
804
  * */
829
805
  append(
830
- element: DOMUtilsTargetElementType | DocumentFragment,
806
+ $el: DOMUtilsTargetElementType | DocumentFragment,
831
807
  content: HTMLElement | string | (HTMLElement | string | Element)[] | NodeList
832
808
  ) {
833
- const DOMUtilsContext = this;
834
- if (typeof element === "string") {
835
- element = DOMUtilsContext.selectorAll(element);
809
+ const that = this;
810
+ if (typeof $el === "string") {
811
+ $el = that.selectorAll($el);
836
812
  }
837
- if (element == null) {
813
+ if ($el == null) {
838
814
  return;
839
815
  }
840
816
 
841
- if (DOMUtilsCommonUtils.isNodeList(element)) {
817
+ if (CommonUtils.isNodeList($el)) {
842
818
  // 设置
843
- element.forEach(($ele) => {
844
- DOMUtilsContext.append($ele as HTMLElement, content);
819
+ $el.forEach(($elItem) => {
820
+ that.append($elItem as HTMLElement, content);
845
821
  });
846
822
  return;
847
823
  }
@@ -849,11 +825,11 @@ class DOMUtils extends DOMUtilsEvent {
849
825
  if (typeof content === "string") {
850
826
  if (ele instanceof DocumentFragment) {
851
827
  if (typeof text === "string") {
852
- text = DOMUtilsContext.parseHTML(text, true, false);
828
+ text = that.toElement(text, true, false);
853
829
  }
854
830
  ele.appendChild(text);
855
831
  } else {
856
- ele.insertAdjacentHTML("beforeend", DOMUtilsCommonUtils.getSafeHTML(text as string));
832
+ ele.insertAdjacentHTML("beforeend", CommonUtils.createSafeHTML(text as string));
857
833
  }
858
834
  } else {
859
835
  ele.appendChild(text as HTMLElement);
@@ -861,220 +837,220 @@ class DOMUtils extends DOMUtilsEvent {
861
837
  }
862
838
  if (Array.isArray(content) || content instanceof NodeList) {
863
839
  /* 数组 */
864
- const fragment = DOMUtilsContext.windowApi.document.createDocumentFragment();
840
+ const fragment = that.windowApi.document.createDocumentFragment();
865
841
  content.forEach((ele) => {
866
842
  if (typeof ele === "string") {
867
843
  // 转为元素
868
- ele = DOMUtilsContext.parseHTML(ele, true, false);
844
+ ele = that.toElement(ele, true, false);
869
845
  }
870
846
  fragment.appendChild(ele);
871
847
  });
872
- element.appendChild(fragment);
848
+ $el.appendChild(fragment);
873
849
  } else {
874
- elementAppendChild(element, content);
850
+ elementAppendChild($el, content);
875
851
  }
876
852
  }
877
853
  /**
878
854
  * 函数 在元素内部开头添加子元素或HTML字符串
879
- * @param element 目标元素
855
+ * @param $el 目标元素
880
856
  * @param content 子元素或HTML字符串
881
857
  * @example
882
858
  * // 元素a.xx内部开头添加一个元素
883
859
  * DOMUtils.prepend(document.querySelector("a.xx"),document.querySelector("b.xx"))
884
860
  * DOMUtils.prepend("a.xx","'<b class="xx"></b>")
885
861
  * */
886
- prepend(element: DOMUtilsTargetElementType | DocumentFragment, content: HTMLElement | string) {
887
- const DOMUtilsContext = this;
888
- if (typeof element === "string") {
889
- element = DOMUtilsContext.selectorAll(element);
862
+ prepend($el: DOMUtilsTargetElementType | DocumentFragment, content: HTMLElement | string) {
863
+ const that = this;
864
+ if (typeof $el === "string") {
865
+ $el = that.selectorAll($el);
890
866
  }
891
- if (element == null) {
867
+ if ($el == null) {
892
868
  return;
893
869
  }
894
- if (DOMUtilsCommonUtils.isNodeList(element)) {
870
+ if (CommonUtils.isNodeList($el)) {
895
871
  // 设置
896
- element.forEach(($ele) => {
897
- DOMUtilsContext.prepend($ele as HTMLElement, content);
872
+ $el.forEach(($elItem) => {
873
+ that.prepend($elItem as HTMLElement, content);
898
874
  });
899
875
  return;
900
876
  }
901
877
  if (typeof content === "string") {
902
- if (element instanceof DocumentFragment) {
903
- content = DOMUtilsContext.parseHTML(content, true, false);
904
- element.prepend(content);
878
+ if ($el instanceof DocumentFragment) {
879
+ content = that.toElement(content, true, false);
880
+ $el.prepend(content);
905
881
  } else {
906
- element.insertAdjacentHTML("afterbegin", DOMUtilsCommonUtils.getSafeHTML(content));
882
+ $el.insertAdjacentHTML("afterbegin", CommonUtils.createSafeHTML(content));
907
883
  }
908
884
  } else {
909
- const $firstChild = element.firstChild;
885
+ const $firstChild = $el.firstChild;
910
886
  if ($firstChild == null) {
911
- element.prepend(content);
887
+ $el.prepend(content);
912
888
  } else {
913
- element.insertBefore(content, element.firstChild);
889
+ $el.insertBefore(content, $firstChild);
914
890
  }
915
891
  }
916
892
  }
917
893
  /**
918
894
  * 在元素后面添加兄弟元素或HTML字符串
919
- * @param element 目标元素
895
+ * @param $el 目标元素
920
896
  * @param content 兄弟元素或HTML字符串
921
897
  * @example
922
898
  * // 元素a.xx后面添加一个元素
923
899
  * DOMUtils.after(document.querySelector("a.xx"),document.querySelector("b.xx"))
924
900
  * DOMUtils.after("a.xx","'<b class="xx"></b>")
925
901
  * */
926
- after(element: DOMUtilsTargetElementType, content: HTMLElement | string) {
927
- const DOMUtilsContext = this;
928
- if (typeof element === "string") {
929
- element = DOMUtilsContext.selectorAll(element);
902
+ after($el: DOMUtilsTargetElementType, content: HTMLElement | string) {
903
+ const that = this;
904
+ if (typeof $el === "string") {
905
+ $el = that.selectorAll($el);
930
906
  }
931
- if (element == null) {
907
+ if ($el == null) {
932
908
  return;
933
909
  }
934
- if (DOMUtilsCommonUtils.isNodeList(element)) {
910
+ if (CommonUtils.isNodeList($el)) {
935
911
  // 设置
936
- element.forEach(($ele) => {
937
- DOMUtilsContext.after($ele as HTMLElement, content);
912
+ $el.forEach(($elItem) => {
913
+ that.after($elItem as HTMLElement, content);
938
914
  });
939
915
  return;
940
916
  }
941
917
  if (typeof content === "string") {
942
- element.insertAdjacentHTML("afterend", DOMUtilsCommonUtils.getSafeHTML(content));
918
+ $el.insertAdjacentHTML("afterend", CommonUtils.createSafeHTML(content));
943
919
  } else {
944
- const $parent = element.parentElement;
945
- const $nextSlibling = element.nextSibling;
920
+ const $parent = $el.parentElement;
921
+ const $nextSlibling = $el.nextSibling;
946
922
  if (!$parent || $nextSlibling) {
947
923
  // 任意一个不行
948
- element.after(content);
924
+ $el.after(content);
949
925
  } else {
950
- element!.parentElement!.insertBefore(content, element.nextSibling);
926
+ $parent.insertBefore(content, $nextSlibling);
951
927
  }
952
928
  }
953
929
  }
954
930
  /**
955
931
  * 在元素前面添加兄弟元素或HTML字符串
956
- * @param element 目标元素
932
+ * @param $el 目标元素
957
933
  * @param content 兄弟元素或HTML字符串
958
934
  * @example
959
935
  * // 元素a.xx前面添加一个元素
960
936
  * DOMUtils.before(document.querySelector("a.xx"),document.querySelector("b.xx"))
961
937
  * DOMUtils.before("a.xx","'<b class="xx"></b>")
962
938
  * */
963
- before(element: DOMUtilsTargetElementType, content: HTMLElement | string) {
964
- const DOMUtilsContext = this;
965
- if (typeof element === "string") {
966
- element = DOMUtilsContext.selectorAll(element);
939
+ before($el: DOMUtilsTargetElementType, content: HTMLElement | string) {
940
+ const that = this;
941
+ if (typeof $el === "string") {
942
+ $el = that.selectorAll($el);
967
943
  }
968
- if (element == null) {
944
+ if ($el == null) {
969
945
  return;
970
946
  }
971
- if (DOMUtilsCommonUtils.isNodeList(element)) {
947
+ if (CommonUtils.isNodeList($el)) {
972
948
  // 设置
973
- element.forEach(($ele) => {
974
- DOMUtilsContext.before($ele as HTMLElement, content);
949
+ $el.forEach(($elItem) => {
950
+ that.before($elItem as HTMLElement, content);
975
951
  });
976
952
  return;
977
953
  }
978
954
  if (typeof content === "string") {
979
- element.insertAdjacentHTML("beforebegin", DOMUtilsCommonUtils.getSafeHTML(content));
955
+ $el.insertAdjacentHTML("beforebegin", CommonUtils.createSafeHTML(content));
980
956
  } else {
981
- const $parent = element.parentElement;
957
+ const $parent = $el.parentElement;
982
958
  if (!$parent) {
983
- element.before(content);
959
+ $el.before(content);
984
960
  } else {
985
- $parent.insertBefore(content, element);
961
+ $parent.insertBefore(content, $el);
986
962
  }
987
963
  }
988
964
  }
989
965
  /**
990
966
  * 移除元素
991
- * @param element 目标元素
967
+ * @param $el 目标元素
992
968
  * @example
993
969
  * // 元素a.xx前面添加一个元素
994
970
  * DOMUtils.remove(document.querySelector("a.xx"))
995
971
  * DOMUtils.remove(document.querySelectorAll("a.xx"))
996
972
  * DOMUtils.remove("a.xx")
997
973
  * */
998
- remove(element: DOMUtilsTargetElementType | Element) {
999
- const DOMUtilsContext = this;
1000
- if (typeof element === "string") {
1001
- element = DOMUtilsContext.selectorAll(element);
974
+ remove($el: DOMUtilsTargetElementType | Element) {
975
+ const that = this;
976
+ if (typeof $el === "string") {
977
+ $el = that.selectorAll($el);
1002
978
  }
1003
- if (element == null) {
979
+ if ($el == null) {
1004
980
  return;
1005
981
  }
1006
- if (DOMUtilsCommonUtils.isNodeList(element)) {
1007
- element.forEach(($ele) => {
1008
- DOMUtilsContext.remove($ele as HTMLElement);
982
+ if (CommonUtils.isNodeList($el)) {
983
+ $el.forEach(($elItem) => {
984
+ that.remove($elItem as HTMLElement);
1009
985
  });
1010
986
  return;
1011
987
  }
1012
- if (typeof element.remove === "function") {
1013
- element.remove();
1014
- } else if (element.parentElement) {
1015
- element.parentElement.removeChild(element);
1016
- } else if (element.parentNode) {
1017
- element.parentNode.removeChild(element);
988
+ if (typeof $el.remove === "function") {
989
+ $el.remove();
990
+ } else if ($el.parentElement) {
991
+ $el.parentElement.removeChild($el);
992
+ } else if ($el.parentNode) {
993
+ $el.parentNode.removeChild($el);
1018
994
  }
1019
995
  }
1020
996
  /**
1021
997
  * 移除元素的所有子元素
1022
- * @param element 目标元素
998
+ * @param $el 目标元素
1023
999
  * @example
1024
1000
  * // 移除元素a.xx元素的所有子元素
1025
1001
  * DOMUtils.empty(document.querySelector("a.xx"))
1026
1002
  * DOMUtils.empty("a.xx")
1027
1003
  * */
1028
- empty(element: DOMUtilsTargetElementType | Element) {
1029
- const DOMUtilsContext = this;
1030
- if (typeof element === "string") {
1031
- element = DOMUtilsContext.selectorAll(element);
1004
+ empty($el: DOMUtilsTargetElementType | Element) {
1005
+ const that = this;
1006
+ if (typeof $el === "string") {
1007
+ $el = that.selectorAll($el);
1032
1008
  }
1033
- if (element == null) {
1009
+ if ($el == null) {
1034
1010
  return;
1035
1011
  }
1036
- if (DOMUtilsCommonUtils.isNodeList(element)) {
1012
+ if (CommonUtils.isNodeList($el)) {
1037
1013
  // 设置
1038
- element.forEach(($ele) => {
1039
- DOMUtilsContext.empty($ele as HTMLElement);
1014
+ $el.forEach(($elItem) => {
1015
+ that.empty($elItem as HTMLElement);
1040
1016
  });
1041
1017
  return;
1042
1018
  }
1043
- if (element.innerHTML) {
1044
- element.innerHTML = "";
1045
- } else if (element.textContent) {
1046
- element.textContent = "";
1019
+ if ($el.innerHTML) {
1020
+ $el.innerHTML = "";
1021
+ } else if ($el.textContent) {
1022
+ $el.textContent = "";
1047
1023
  }
1048
1024
  }
1049
1025
  /**
1050
1026
  * 获取元素相对于文档的偏移坐标(加上文档的滚动条)
1051
- * @param element 目标元素
1027
+ * @param $el 目标元素
1052
1028
  * @example
1053
1029
  * // 获取元素a.xx的对于文档的偏移坐标
1054
1030
  * DOMUtils.offset(document.querySelector("a.xx"))
1055
1031
  * DOMUtils.offset("a.xx")
1056
1032
  * > 0
1057
1033
  */
1058
- offset(element: HTMLElement | string) {
1059
- const DOMUtilsContext = this;
1060
- if (typeof element === "string") {
1061
- element = DOMUtilsContext.selector(element) as HTMLElement;
1034
+ offset($el: HTMLElement | string) {
1035
+ const that = this;
1036
+ if (typeof $el === "string") {
1037
+ $el = that.selector($el) as HTMLElement;
1062
1038
  }
1063
- if (element == null) {
1039
+ if ($el == null) {
1064
1040
  return;
1065
1041
  }
1066
1042
 
1067
- const rect = element.getBoundingClientRect();
1043
+ const rect = $el.getBoundingClientRect();
1068
1044
  return {
1069
1045
  /** y轴偏移 */
1070
- top: rect.top + DOMUtilsContext.windowApi.globalThis.scrollY,
1046
+ top: rect.top + that.windowApi.globalThis.scrollY,
1071
1047
  /** x轴偏移 */
1072
- left: rect.left + DOMUtilsContext.windowApi.globalThis.scrollX,
1048
+ left: rect.left + that.windowApi.globalThis.scrollX,
1073
1049
  };
1074
1050
  }
1075
1051
  /**
1076
1052
  * 获取元素的宽度
1077
- * @param element 要获取宽度的元素
1053
+ * @param $el 要获取宽度的元素
1078
1054
  * @param value 宽度值
1079
1055
  * @param isShow 是否已进行isShow,避免爆堆栈
1080
1056
  * @returns 元素的宽度,单位为像素
@@ -1091,43 +1067,43 @@ class DOMUtils extends DOMUtilsEvent {
1091
1067
  * DOMUtils.width(document.querySelector("a.xx"),200)
1092
1068
  * DOMUtils.width("a.xx",200)
1093
1069
  */
1094
- width(element: HTMLElement | string | Window | typeof globalThis | Document, isShow?: boolean): number;
1095
- width(element: HTMLElement | string | Window | typeof globalThis | Document, isShow: boolean = false): number {
1096
- const DOMUtilsContext = this;
1097
- if (typeof element === "string") {
1098
- element = DOMUtilsContext.selector<HTMLElement>(element)!;
1070
+ width($el: HTMLElement | string | Window | typeof globalThis | Document, isShow?: boolean): number;
1071
+ width($el: HTMLElement | string | Window | typeof globalThis | Document, isShow: boolean = false): number {
1072
+ const that = this;
1073
+ if (typeof $el === "string") {
1074
+ $el = that.selector<HTMLElement>($el)!;
1099
1075
  }
1100
- if (DOMUtilsCommonUtils.isWin(element)) {
1101
- return DOMUtilsContext.windowApi.window.document.documentElement.clientWidth;
1076
+ if (CommonUtils.isWin($el)) {
1077
+ return that.windowApi.window.document.documentElement.clientWidth;
1102
1078
  }
1103
- if ((element as HTMLElement).nodeType === 9) {
1079
+ if (($el as HTMLElement).nodeType === 9) {
1104
1080
  /* Document文档节点 */
1105
- element = element as Document;
1081
+ $el = $el as Document;
1106
1082
  return Math.max(
1107
- element.body.scrollWidth,
1108
- element.documentElement.scrollWidth,
1109
- element.body.offsetWidth,
1110
- element.documentElement.offsetWidth,
1111
- element.documentElement.clientWidth
1083
+ $el.body.scrollWidth,
1084
+ $el.documentElement.scrollWidth,
1085
+ $el.body.offsetWidth,
1086
+ $el.documentElement.offsetWidth,
1087
+ $el.documentElement.clientWidth
1112
1088
  );
1113
1089
  }
1114
- if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element as HTMLElement))) {
1090
+ if (isShow || (!isShow && CommonUtils.isShow($el as HTMLElement))) {
1115
1091
  /* 已显示 */
1116
1092
  /* 不从style中获取对应的宽度,因为可能使用了class定义了width !important */
1117
- element = element as HTMLElement;
1093
+ $el = $el as HTMLElement;
1118
1094
  /* 如果element.style.width为空 则从css里面获取是否定义了width信息如果定义了 则读取css里面定义的宽度width */
1119
- if (parseFloat(DOMUtilsCommonUtils.getStyleValue(element, "width").toString()) > 0) {
1120
- return parseFloat(DOMUtilsCommonUtils.getStyleValue(element, "width").toString());
1095
+ if (parseFloat(CommonUtils.getStyleValue($el, "width").toString()) > 0) {
1096
+ return parseFloat(CommonUtils.getStyleValue($el, "width").toString());
1121
1097
  }
1122
1098
 
1123
1099
  /* 如果从css里获取到的值不是大于0 可能是auto 则通过offsetWidth来进行计算 */
1124
- if (element.offsetWidth > 0) {
1125
- const borderLeftWidth = DOMUtilsCommonUtils.getStyleValue(element, "borderLeftWidth");
1126
- const borderRightWidth = DOMUtilsCommonUtils.getStyleValue(element, "borderRightWidth");
1127
- const paddingLeft = DOMUtilsCommonUtils.getStyleValue(element, "paddingLeft");
1128
- const paddingRight = DOMUtilsCommonUtils.getStyleValue(element, "paddingRight");
1100
+ if ($el.offsetWidth > 0) {
1101
+ const borderLeftWidth = CommonUtils.getStyleValue($el, "borderLeftWidth");
1102
+ const borderRightWidth = CommonUtils.getStyleValue($el, "borderRightWidth");
1103
+ const paddingLeft = CommonUtils.getStyleValue($el, "paddingLeft");
1104
+ const paddingRight = CommonUtils.getStyleValue($el, "paddingRight");
1129
1105
  const backHeight =
1130
- parseFloat(element.offsetWidth.toString()) -
1106
+ parseFloat($el.offsetWidth.toString()) -
1131
1107
  parseFloat(borderLeftWidth.toString()) -
1132
1108
  parseFloat(borderRightWidth.toString()) -
1133
1109
  parseFloat(paddingLeft.toString()) -
@@ -1137,9 +1113,9 @@ class DOMUtils extends DOMUtilsEvent {
1137
1113
  return 0;
1138
1114
  } else {
1139
1115
  /* 未显示 */
1140
- element = element as HTMLElement;
1141
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
1142
- const width = DOMUtilsContext.width(element, true);
1116
+ $el = $el as HTMLElement;
1117
+ const { recovery } = CommonUtils.forceShow($el);
1118
+ const width = that.width($el, true);
1143
1119
  recovery();
1144
1120
  return width;
1145
1121
  }
@@ -1147,7 +1123,7 @@ class DOMUtils extends DOMUtilsEvent {
1147
1123
 
1148
1124
  /**
1149
1125
  * 获取元素的高度
1150
- * @param element 要获取高度的元素
1126
+ * @param $el 要获取高度的元素
1151
1127
  * @param isShow 是否已进行isShow,避免爆堆栈
1152
1128
  * @returns 元素的高度,单位为像素
1153
1129
  * @example
@@ -1163,43 +1139,43 @@ class DOMUtils extends DOMUtilsEvent {
1163
1139
  * DOMUtils.height(document.querySelector("a.xx"),200)
1164
1140
  * DOMUtils.height("a.xx",200)
1165
1141
  */
1166
- height(element: HTMLElement | string | Window | typeof globalThis | Document, isShow?: boolean): number;
1167
- height(element: HTMLElement | string | Window | typeof globalThis | Document, isShow: boolean = false): number {
1168
- const DOMUtilsContext = this;
1169
- if (DOMUtilsCommonUtils.isWin(element)) {
1170
- return DOMUtilsContext.windowApi.window.document.documentElement.clientHeight;
1142
+ height($el: HTMLElement | string | Window | typeof globalThis | Document, isShow?: boolean): number;
1143
+ height($el: HTMLElement | string | Window | typeof globalThis | Document, isShow: boolean = false): number {
1144
+ const that = this;
1145
+ if (CommonUtils.isWin($el)) {
1146
+ return that.windowApi.window.document.documentElement.clientHeight;
1171
1147
  }
1172
- if (typeof element === "string") {
1173
- element = DOMUtilsContext.selector(element) as HTMLElement;
1148
+ if (typeof $el === "string") {
1149
+ $el = that.selector($el) as HTMLElement;
1174
1150
  }
1175
- if ((element as Document).nodeType === 9) {
1176
- element = element as Document;
1151
+ if (($el as Document).nodeType === 9) {
1152
+ $el = $el as Document;
1177
1153
  /* Document文档节点 */
1178
1154
  return Math.max(
1179
- element.body.scrollHeight,
1180
- element.documentElement.scrollHeight,
1181
- element.body.offsetHeight,
1182
- element.documentElement.offsetHeight,
1183
- element.documentElement.clientHeight
1155
+ $el.body.scrollHeight,
1156
+ $el.documentElement.scrollHeight,
1157
+ $el.body.offsetHeight,
1158
+ $el.documentElement.offsetHeight,
1159
+ $el.documentElement.clientHeight
1184
1160
  );
1185
1161
  }
1186
- if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element as HTMLElement))) {
1187
- element = element as HTMLElement;
1162
+ if (isShow || (!isShow && CommonUtils.isShow($el as HTMLElement))) {
1163
+ $el = $el as HTMLElement;
1188
1164
  /* 已显示 */
1189
1165
  /* 从style中获取对应的高度,因为可能使用了class定义了width !important */
1190
1166
  /* 如果element.style.height为空 则从css里面获取是否定义了height信息如果定义了 则读取css里面定义的高度height */
1191
- if (parseFloat(DOMUtilsCommonUtils.getStyleValue(element, "height").toString()) > 0) {
1192
- return parseFloat(DOMUtilsCommonUtils.getStyleValue(element, "height").toString());
1167
+ if (parseFloat(CommonUtils.getStyleValue($el, "height").toString()) > 0) {
1168
+ return parseFloat(CommonUtils.getStyleValue($el, "height").toString());
1193
1169
  }
1194
1170
 
1195
1171
  /* 如果从css里获取到的值不是大于0 可能是auto 则通过offsetHeight来进行计算 */
1196
- if (element.offsetHeight > 0) {
1197
- const borderTopWidth = DOMUtilsCommonUtils.getStyleValue(element, "borderTopWidth");
1198
- const borderBottomWidth = DOMUtilsCommonUtils.getStyleValue(element, "borderBottomWidth");
1199
- const paddingTop = DOMUtilsCommonUtils.getStyleValue(element, "paddingTop");
1200
- const paddingBottom = DOMUtilsCommonUtils.getStyleValue(element, "paddingBottom");
1172
+ if ($el.offsetHeight > 0) {
1173
+ const borderTopWidth = CommonUtils.getStyleValue($el, "borderTopWidth");
1174
+ const borderBottomWidth = CommonUtils.getStyleValue($el, "borderBottomWidth");
1175
+ const paddingTop = CommonUtils.getStyleValue($el, "paddingTop");
1176
+ const paddingBottom = CommonUtils.getStyleValue($el, "paddingBottom");
1201
1177
  const backHeight =
1202
- parseFloat(element.offsetHeight.toString()) -
1178
+ parseFloat($el.offsetHeight.toString()) -
1203
1179
  parseFloat(borderTopWidth.toString()) -
1204
1180
  parseFloat(borderBottomWidth.toString()) -
1205
1181
  parseFloat(paddingTop.toString()) -
@@ -1209,16 +1185,16 @@ class DOMUtils extends DOMUtilsEvent {
1209
1185
  return 0;
1210
1186
  } else {
1211
1187
  /* 未显示 */
1212
- element = element as HTMLElement;
1213
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
1214
- const height = DOMUtilsContext.height(element, true);
1188
+ $el = $el as HTMLElement;
1189
+ const { recovery } = CommonUtils.forceShow($el);
1190
+ const height = that.height($el, true);
1215
1191
  recovery();
1216
1192
  return height;
1217
1193
  }
1218
1194
  }
1219
1195
  /**
1220
1196
  * 获取元素的外部宽度(包括边框和外边距)
1221
- * @param element 要获取外部宽度的元素
1197
+ * @param $el 要获取外部宽度的元素
1222
1198
  * @param [isShow=false] 是否已进行isShow,避免爆堆栈
1223
1199
  * @returns 元素的外部宽度,单位为像素
1224
1200
  * @example
@@ -1230,31 +1206,31 @@ class DOMUtils extends DOMUtilsEvent {
1230
1206
  * DOMUtils.outerWidth(window)
1231
1207
  * > 400
1232
1208
  */
1233
- outerWidth(element: HTMLElement | string | Window | typeof globalThis | Document, isShow?: boolean): number;
1234
- outerWidth(element: HTMLElement | string | Window | typeof globalThis | Document, isShow: boolean = false): number {
1235
- const DOMUtilsContext = this;
1236
- if (DOMUtilsCommonUtils.isWin(element)) {
1237
- return DOMUtilsContext.windowApi.window.innerWidth;
1238
- }
1239
- if (typeof element === "string") {
1240
- element = DOMUtilsContext.selector(element) as HTMLElement;
1241
- }
1242
- element = element as HTMLElement;
1243
- if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
1244
- const style = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element, null);
1245
- const marginLeft = DOMUtilsCommonUtils.getStyleValue(style, "marginLeft");
1246
- const marginRight = DOMUtilsCommonUtils.getStyleValue(style, "marginRight");
1247
- return element.offsetWidth + marginLeft + marginRight;
1209
+ outerWidth($el: HTMLElement | string | Window | typeof globalThis | Document, isShow?: boolean): number;
1210
+ outerWidth($el: HTMLElement | string | Window | typeof globalThis | Document, isShow: boolean = false): number {
1211
+ const that = this;
1212
+ if (CommonUtils.isWin($el)) {
1213
+ return that.windowApi.window.innerWidth;
1214
+ }
1215
+ if (typeof $el === "string") {
1216
+ $el = that.selector($el) as HTMLElement;
1217
+ }
1218
+ $el = $el as HTMLElement;
1219
+ if (isShow || (!isShow && CommonUtils.isShow($el))) {
1220
+ const style = that.windowApi.globalThis.getComputedStyle($el, null);
1221
+ const marginLeft = CommonUtils.getStyleValue(style, "marginLeft");
1222
+ const marginRight = CommonUtils.getStyleValue(style, "marginRight");
1223
+ return $el.offsetWidth + marginLeft + marginRight;
1248
1224
  } else {
1249
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
1250
- const outerWidth = DOMUtilsContext.outerWidth(element, true);
1225
+ const { recovery } = CommonUtils.forceShow($el);
1226
+ const outerWidth = that.outerWidth($el, true);
1251
1227
  recovery();
1252
1228
  return outerWidth;
1253
1229
  }
1254
1230
  }
1255
1231
  /**
1256
1232
  * 获取元素的外部高度(包括边框和外边距)
1257
- * @param {HTMLElement|string} element 要获取外部高度的元素
1233
+ * @param {HTMLElement|string} $el 要获取外部高度的元素
1258
1234
  * @param {boolean} [isShow=false] 是否已进行isShow,避免爆堆栈
1259
1235
  * @returns {number} 元素的外部高度,单位为像素
1260
1236
  * @example
@@ -1266,139 +1242,102 @@ class DOMUtils extends DOMUtilsEvent {
1266
1242
  * DOMUtils.outerHeight(window)
1267
1243
  * > 700
1268
1244
  */
1269
- outerHeight(element: HTMLElement | string | Window | typeof globalThis | Document, isShow?: boolean): number;
1270
- outerHeight(element: HTMLElement | string | Window | typeof globalThis | Document, isShow: boolean = false): number {
1271
- const DOMUtilsContext = this;
1272
- if (DOMUtilsCommonUtils.isWin(element)) {
1273
- return DOMUtilsContext.windowApi.window.innerHeight;
1274
- }
1275
- if (typeof element === "string") {
1276
- element = DOMUtilsContext.selector(element) as HTMLElement;
1277
- }
1278
- element = element as HTMLElement;
1279
- if (isShow || (!isShow && DOMUtilsCommonUtils.isShow(element))) {
1280
- const style = DOMUtilsContext.windowApi.globalThis.getComputedStyle(element, null);
1281
- const marginTop = DOMUtilsCommonUtils.getStyleValue(style, "marginTop");
1282
- const marginBottom = DOMUtilsCommonUtils.getStyleValue(style, "marginBottom");
1283
- return element.offsetHeight + marginTop + marginBottom;
1245
+ outerHeight($el: HTMLElement | string | Window | typeof globalThis | Document, isShow?: boolean): number;
1246
+ outerHeight($el: HTMLElement | string | Window | typeof globalThis | Document, isShow: boolean = false): number {
1247
+ const that = this;
1248
+ if (CommonUtils.isWin($el)) {
1249
+ return that.windowApi.window.innerHeight;
1250
+ }
1251
+ if (typeof $el === "string") {
1252
+ $el = that.selector($el) as HTMLElement;
1253
+ }
1254
+ $el = $el as HTMLElement;
1255
+ if (isShow || (!isShow && CommonUtils.isShow($el))) {
1256
+ const style = that.windowApi.globalThis.getComputedStyle($el, null);
1257
+ const marginTop = CommonUtils.getStyleValue(style, "marginTop");
1258
+ const marginBottom = CommonUtils.getStyleValue(style, "marginBottom");
1259
+ return $el.offsetHeight + marginTop + marginBottom;
1284
1260
  } else {
1285
- const { recovery } = DOMUtilsCommonUtils.showElement(element);
1286
- const outerHeight = DOMUtilsContext.outerHeight(element, true);
1261
+ const { recovery } = CommonUtils.forceShow($el);
1262
+ const outerHeight = that.outerHeight($el, true);
1287
1263
  recovery();
1288
1264
  return outerHeight;
1289
1265
  }
1290
1266
  }
1291
1267
  /**
1292
- * 在一定时间内改变元素的样式属性,实现动画效果
1293
- * @param element 需要进行动画的元素
1294
- * @param styles 动画结束时元素的样式属性
1295
- * @param duration 动画持续时间,单位为毫秒
1296
- * @param callback 动画结束后执行的函数
1268
+ * 将一个元素替换为另一个元素
1269
+ * @param $el 目标元素
1270
+ * @param $newEl 新元素
1297
1271
  * @example
1298
- * // 监听元素a.xx的从显示变为隐藏
1299
- * DOMUtils.animate(document.querySelector("a.xx"),{ top:100},1000,function(){
1300
- * console.log("已往上位移100px")
1301
- * })
1272
+ * // 替换元素a.xx为b.xx
1273
+ * DOMUtils.replaceWith(document.querySelector("a.xx"),document.querySelector("b.xx"))
1274
+ * DOMUtils.replaceWith("a.xx",'<b class="xx"></b>')
1302
1275
  */
1303
- animate(
1304
- element: DOMUtilsTargetElementType,
1305
- styles: CSSStyleDeclaration,
1306
- duration: number = 1000,
1307
- callback: (() => void) | undefined | null = null
1308
- ) {
1309
- const DOMUtilsContext = this;
1310
- if (typeof element === "string") {
1311
- element = DOMUtilsContext.selectorAll(element);
1276
+ replaceWith($el: DOMUtilsTargetElementType, $newEl: HTMLElement | string | Node) {
1277
+ const that = this;
1278
+ if (typeof $el === "string") {
1279
+ $el = that.selectorAll($el);
1312
1280
  }
1313
- if (element == null) {
1281
+ if ($el == null) {
1314
1282
  return;
1315
1283
  }
1316
- if (DOMUtilsCommonUtils.isNodeList(element)) {
1284
+ if (CommonUtils.isNodeList($el)) {
1317
1285
  // 设置
1318
- element.forEach(($ele) => {
1319
- DOMUtilsContext.animate($ele as HTMLElement, styles, duration, callback);
1286
+ $el.forEach(($elItem) => {
1287
+ that.replaceWith($elItem as HTMLElement, $newEl);
1320
1288
  });
1321
1289
  return;
1322
1290
  }
1323
- if (typeof duration !== "number" || duration <= 0) {
1324
- throw new TypeError("duration must be a positive number");
1325
- }
1326
- if (typeof callback !== "function" && callback !== void 0) {
1327
- throw new TypeError("callback must be a function or null");
1328
- }
1329
- if (typeof styles !== "object" || styles === void 0) {
1330
- throw new TypeError("styles must be an object");
1331
- }
1332
- if (Object.keys(styles).length === 0) {
1333
- throw new Error("styles must contain at least one property");
1334
- }
1335
- const start = performance.now();
1336
- const from: {
1337
- [prop: string]: any;
1338
- } = {};
1339
- const to: {
1340
- [prop: string]: any;
1341
- } = {};
1342
- for (const prop in styles) {
1343
- from[prop] = element.style[prop] || DOMUtilsContext.windowApi.globalThis.getComputedStyle(element)[prop];
1344
- to[prop] = styles[prop];
1345
- }
1346
- const timer = DOMUtilsCommonUtils.setInterval(function () {
1347
- const timePassed = performance.now() - start;
1348
- let progress = timePassed / duration;
1349
- if (progress > 1) {
1350
- progress = 1;
1351
- }
1352
- for (const prop in styles) {
1353
- element.style[prop] = from[prop] + (to[prop] - from[prop]) * progress + "px";
1354
- }
1355
- if (progress === 1) {
1356
- DOMUtilsCommonUtils.clearInterval(timer);
1357
- if (callback) {
1358
- callback();
1359
- }
1360
- }
1361
- }, 10);
1291
+ if (typeof $newEl === "string") {
1292
+ $newEl = that.toElement($newEl, false, false);
1293
+ }
1294
+ const $parent = $el.parentElement;
1295
+ if ($parent) {
1296
+ $parent.replaceChild($newEl as Node, $el);
1297
+ } else {
1298
+ that.after($el, $newEl as HTMLElement);
1299
+ $el.remove();
1300
+ }
1362
1301
  }
1363
1302
  /**
1364
1303
  * 将一个元素包裹在指定的HTML元素中
1365
- * @param element 要包裹的元素
1304
+ * @param $el 要包裹的元素
1366
1305
  * @param wrapperHTML 要包裹的HTML元素的字符串表示形式
1367
1306
  * @example
1368
1307
  * // 将a.xx元素外面包裹一层div
1369
1308
  * DOMUtils.wrap(document.querySelector("a.xx"),"<div></div>")
1370
1309
  */
1371
- wrap(element: DOMUtilsTargetElementType, wrapperHTML: string) {
1372
- const DOMUtilsContext = this;
1373
- if (typeof element === "string") {
1374
- element = DOMUtilsContext.selectorAll(element);
1310
+ wrap($el: DOMUtilsTargetElementType, wrapperHTML: string) {
1311
+ const that = this;
1312
+ if (typeof $el === "string") {
1313
+ $el = that.selectorAll($el);
1375
1314
  }
1376
- if (element == null) {
1315
+ if ($el == null) {
1377
1316
  return;
1378
1317
  }
1379
- if (DOMUtilsCommonUtils.isNodeList(element)) {
1318
+ if (CommonUtils.isNodeList($el)) {
1380
1319
  // 设置
1381
- element.forEach(($ele) => {
1382
- DOMUtilsContext.wrap($ele as HTMLElement, wrapperHTML);
1320
+ $el.forEach(($elItem) => {
1321
+ that.wrap($elItem as HTMLElement, wrapperHTML);
1383
1322
  });
1384
1323
  return;
1385
1324
  }
1386
- element = element as HTMLElement;
1325
+ $el = $el as HTMLElement;
1387
1326
  // 创建一个新的div元素,并将wrapperHTML作为其innerHTML
1388
- const wrapper = DOMUtilsContext.windowApi.document.createElement("div");
1389
- DOMUtilsContext.html(wrapper, wrapperHTML);
1327
+ const $wrapper = that.windowApi.document.createElement("div");
1328
+ that.html($wrapper, wrapperHTML);
1390
1329
 
1391
- const wrapperFirstChild = wrapper.firstChild as HTMLElement;
1330
+ const wrapperFirstChild = $wrapper.firstChild as HTMLElement;
1392
1331
  // 将要包裹的元素插入目标元素前面
1393
- const parentElement = element.parentElement as HTMLElement;
1394
- parentElement.insertBefore(wrapperFirstChild, element);
1332
+ const parentElement = $el.parentElement as HTMLElement;
1333
+ parentElement.insertBefore(wrapperFirstChild, $el);
1395
1334
 
1396
1335
  // 将要包裹的元素移动到wrapper中
1397
- wrapperFirstChild.appendChild(element);
1336
+ wrapperFirstChild.appendChild($el);
1398
1337
  }
1399
1338
  /**
1400
1339
  * 获取当前元素的前一个兄弟元素
1401
- * @param element 当前元素
1340
+ * @param $el 当前元素
1402
1341
  * @returns 前一个兄弟元素
1403
1342
  * @example
1404
1343
  * // 获取a.xx元素前一个兄弟元素
@@ -1406,21 +1345,20 @@ class DOMUtils extends DOMUtilsEvent {
1406
1345
  * DOMUtils.prev("a.xx")
1407
1346
  * > <div ...>....</div>
1408
1347
  */
1409
- prev(element: HTMLElement | string): HTMLElement;
1410
- prev(element: HTMLElement | string) {
1411
- const DOMUtilsContext = this;
1412
- if (typeof element === "string") {
1413
- element = DOMUtilsContext.selector(element) as HTMLElement;
1348
+ prev($el: HTMLElement | string): HTMLElement;
1349
+ prev($el: HTMLElement | string) {
1350
+ const that = this;
1351
+ if (typeof $el === "string") {
1352
+ $el = that.selector($el) as HTMLElement;
1414
1353
  }
1415
- if (element == null) {
1354
+ if ($el == null) {
1416
1355
  return;
1417
1356
  }
1418
- return element.previousElementSibling as HTMLElement;
1357
+ return $el.previousElementSibling as HTMLElement;
1419
1358
  }
1420
-
1421
1359
  /**
1422
1360
  * 获取当前元素的后一个兄弟元素
1423
- * @param element 当前元素
1361
+ * @param $el 当前元素
1424
1362
  * @returns 后一个兄弟元素
1425
1363
  * @example
1426
1364
  * // 获取a.xx元素前一个兄弟元素
@@ -1428,29 +1366,16 @@ class DOMUtils extends DOMUtilsEvent {
1428
1366
  * DOMUtils.next("a.xx")
1429
1367
  * > <div ...>....</div>
1430
1368
  */
1431
- next(element: HTMLElement | string): HTMLElement;
1432
- next(element: HTMLElement | string) {
1433
- const DOMUtilsContext = this;
1434
- if (typeof element === "string") {
1435
- element = DOMUtilsContext.selector(element) as HTMLElement;
1369
+ next($el: HTMLElement | string): HTMLElement;
1370
+ next($el: HTMLElement | string) {
1371
+ const that = this;
1372
+ if (typeof $el === "string") {
1373
+ $el = that.selector($el) as HTMLElement;
1436
1374
  }
1437
- if (element == null) {
1375
+ if ($el == null) {
1438
1376
  return;
1439
1377
  }
1440
- return element.nextElementSibling as HTMLElement;
1441
- }
1442
- /**
1443
- * 取消挂载在window下的DOMUtils并返回DOMUtils
1444
- * @example
1445
- * let DOMUtils = window.DOMUtils.noConflict()
1446
- */
1447
- noConflict() {
1448
- const DOMUtilsContext = this;
1449
- if ((DOMUtilsContext.windowApi.window as any).DOMUtils) {
1450
- DOMUtilsCommonUtils.delete(window, "DOMUtils");
1451
- }
1452
- (DOMUtilsContext.windowApi.window as any).DOMUtils = this;
1453
- return this;
1378
+ return $el.nextElementSibling as HTMLElement;
1454
1379
  }
1455
1380
  /**
1456
1381
  * 获取当前元素的所有兄弟元素
@@ -1463,21 +1388,21 @@ class DOMUtils extends DOMUtilsEvent {
1463
1388
  * > (3)[div.logo-wrapper, div.forum-block, div.more-btn-desc]
1464
1389
  */
1465
1390
  siblings(element: HTMLElement | string): HTMLElement[];
1466
- siblings(element: HTMLElement | string) {
1467
- const DOMUtilsContext = this;
1468
- if (typeof element === "string") {
1469
- element = DOMUtilsContext.selector(element) as HTMLElement;
1391
+ siblings($el: HTMLElement | string) {
1392
+ const that = this;
1393
+ if (typeof $el === "string") {
1394
+ $el = that.selector($el) as HTMLElement;
1470
1395
  }
1471
- if (element == null) {
1396
+ if ($el == null) {
1472
1397
  return;
1473
1398
  }
1474
- return Array.from((element.parentElement as HTMLElement).children as HTMLCollectionOf<HTMLElement>).filter(
1475
- (child) => child !== element
1399
+ return Array.from(($el.parentElement as HTMLElement).children as HTMLCollectionOf<HTMLElement>).filter(
1400
+ ($child) => $child !== $el
1476
1401
  );
1477
1402
  }
1478
1403
  /**
1479
1404
  * 获取当前元素的父元素
1480
- * @param element 当前元素
1405
+ * @param $el 当前元素
1481
1406
  * @returns 父元素
1482
1407
  * @example
1483
1408
  * // 获取a.xx元素的父元素
@@ -1485,10 +1410,10 @@ class DOMUtils extends DOMUtilsEvent {
1485
1410
  * DOMUtils.parent("a.xx")
1486
1411
  * > <div ...>....</div>
1487
1412
  */
1488
- parent(element: HTMLElement | string): HTMLElement;
1413
+ parent($el: HTMLElement | string): HTMLElement;
1489
1414
  /**
1490
1415
  * 获取当前元素的父元素
1491
- * @param element 当前元素
1416
+ * @param $el 当前元素
1492
1417
  * @returns 父元素
1493
1418
  * @example
1494
1419
  * // 获取a.xx元素的父元素
@@ -1496,10 +1421,10 @@ class DOMUtils extends DOMUtilsEvent {
1496
1421
  * DOMUtils.parent("a.xx")
1497
1422
  * > <div ...>....</div>
1498
1423
  */
1499
- parent(element: HTMLElement[] | NodeList): HTMLElement[];
1424
+ parent($el: HTMLElement[] | NodeList): HTMLElement[];
1500
1425
  /**
1501
1426
  * 获取当前元素的父元素
1502
- * @param element 当前元素
1427
+ * @param $el 当前元素
1503
1428
  * @returns 父元素
1504
1429
  * @example
1505
1430
  * // 获取a.xx元素的父元素
@@ -1507,22 +1432,22 @@ class DOMUtils extends DOMUtilsEvent {
1507
1432
  * DOMUtils.parent("a.xx")
1508
1433
  * > <div ...>....</div>
1509
1434
  */
1510
- parent(element: HTMLElement | Element | Node | NodeList | string | HTMLElement[]) {
1511
- const DOMUtilsContext = this;
1512
- if (typeof element === "string") {
1513
- element = DOMUtilsContext.selector<HTMLElement>(element)!;
1435
+ parent($el: HTMLElement | Element | Node | NodeList | string | HTMLElement[]) {
1436
+ const that = this;
1437
+ if (typeof $el === "string") {
1438
+ $el = that.selector<HTMLElement>($el)!;
1514
1439
  }
1515
- if (element == null) {
1440
+ if ($el == null) {
1516
1441
  return;
1517
1442
  }
1518
- if (DOMUtilsCommonUtils.isNodeList(element)) {
1443
+ if (CommonUtils.isNodeList($el)) {
1519
1444
  const resultArray: HTMLElement[] = [];
1520
- element.forEach(($ele) => {
1521
- resultArray.push(DOMUtilsContext.parent($ele as HTMLElement));
1445
+ $el.forEach(($elItem) => {
1446
+ resultArray.push(that.parent($elItem as HTMLElement));
1522
1447
  });
1523
1448
  return resultArray;
1524
1449
  } else {
1525
- return element.parentElement;
1450
+ return $el.parentElement;
1526
1451
  }
1527
1452
  }
1528
1453
  /**
@@ -1538,28 +1463,28 @@ class DOMUtils extends DOMUtilsEvent {
1538
1463
  * 如果useParser为false,返回一个DIV元素的firstChild
1539
1464
  * @example
1540
1465
  * // 将字符串转为Element元素
1541
- * DOMUtils.parseHTML("<a href='xxxx'></a>")
1466
+ * DOMUtils.toElement("<a href='xxxx'></a>")
1542
1467
  * > <a href="xxxx"></a>
1543
1468
  * @example
1544
1469
  * // 使用DOMParser将字符串转为Element元素
1545
- * DOMUtils.parseHTML("<a href='xxxx'></a>",true)
1470
+ * DOMUtils.toElement("<a href='xxxx'></a>",true)
1546
1471
  * > <a href="xxxx"></a>
1547
1472
  * @example
1548
1473
  * // 由于需要转换的元素是多个元素,将字符串转为完整的Element元素
1549
- * DOMUtils.parseHTML("<a href='xxxx'></a><a href='xxxx'></a>",false, true)
1474
+ * DOMUtils.toElement("<a href='xxxx'></a><a href='xxxx'></a>",false, true)
1550
1475
  * > <div><a href="xxxx"></a><a href='xxxx'></a></div>
1551
1476
  * @example
1552
1477
  * // 由于需要转换的元素是多个元素,使用DOMParser将字符串转为完整的Element元素
1553
- * DOMUtils.parseHTML("<a href='xxxx'></a><a href='xxxx'></a>",true, true)
1478
+ * DOMUtils.toElement("<a href='xxxx'></a><a href='xxxx'></a>",true, true)
1554
1479
  * > #document
1555
1480
  */
1556
- parseHTML<T1 extends boolean, T2 extends boolean>(
1481
+ toElement<T1 extends boolean, T2 extends boolean>(
1557
1482
  html: string,
1558
1483
  useParser?: T1,
1559
1484
  isComplete?: T2
1560
- ): ParseHTMLReturnType<T1, T2>;
1561
- parseHTML(html: string, useParser = false, isComplete = false) {
1562
- const DOMUtilsContext = this;
1485
+ ): T1 extends true ? (T2 extends true ? Document : HTMLElement) : HTMLElement;
1486
+ toElement(html: string, useParser = false, isComplete = false) {
1487
+ const that = this;
1563
1488
  // 去除html前后的空格
1564
1489
  html = html.trim();
1565
1490
  function parseHTMLByDOMParser() {
@@ -1571,12 +1496,12 @@ class DOMUtils extends DOMUtilsEvent {
1571
1496
  }
1572
1497
  }
1573
1498
  function parseHTMLByCreateDom() {
1574
- const tempDIV = DOMUtilsContext.windowApi.document.createElement("div");
1575
- DOMUtilsContext.html(tempDIV, html);
1499
+ const $el = that.windowApi.document.createElement("div");
1500
+ that.html($el, html);
1576
1501
  if (isComplete) {
1577
- return tempDIV;
1502
+ return $el;
1578
1503
  } else {
1579
- return tempDIV.firstChild;
1504
+ return $el.firstElementChild ?? $el.firstChild;
1580
1505
  }
1581
1506
  }
1582
1507
  if (useParser) {
@@ -1624,206 +1549,6 @@ class DOMUtils extends DOMUtilsEvent {
1624
1549
  .map((item) => `${encodeURIComponent(item.name)}=${encodeURIComponent(item.value)}`)
1625
1550
  .join("&");
1626
1551
  }
1627
- /**
1628
- * 显示元素
1629
- * @param target 当前元素
1630
- * @param checkVisiblie 是否检测元素是否显示
1631
- * + true (默认)如果检测到还未显示,则强制使用display: unset !important;
1632
- * + false 不检测,直接设置display属性为空
1633
- * @example
1634
- * // 显示a.xx元素
1635
- * DOMUtils.show(document.querySelector("a.xx"))
1636
- * DOMUtils.show(document.querySelectorAll("a.xx"))
1637
- * DOMUtils.show("a.xx")
1638
- */
1639
- show(target: DOMUtilsTargetElementType, checkVisiblie: boolean = true) {
1640
- const DOMUtilsContext = this;
1641
- if (target == null) {
1642
- return;
1643
- }
1644
- if (typeof target === "string") {
1645
- target = DOMUtilsContext.selectorAll(target);
1646
- }
1647
- if (target instanceof NodeList || target instanceof Array) {
1648
- target = target as HTMLElement[];
1649
- for (const element of target) {
1650
- DOMUtilsContext.show(element, checkVisiblie);
1651
- }
1652
- } else {
1653
- target = target as HTMLElement;
1654
- target.style.display = "";
1655
- if (checkVisiblie) {
1656
- if (!DOMUtilsCommonUtils.isShow(target)) {
1657
- /* 仍然是不显示,尝试使用强覆盖 */
1658
- target.style.setProperty("display", "unset", "important");
1659
- }
1660
- }
1661
- }
1662
- }
1663
- /**
1664
- * 隐藏元素
1665
- * @param target 当前元素
1666
- * @param checkVisiblie 是否检测元素是否显示
1667
- * + true (默认)如果检测到显示,则强制使用display: none !important;
1668
- * + false 不检测,直接设置display属性为none
1669
- * @example
1670
- * // 隐藏a.xx元素
1671
- * DOMUtils.hide(document.querySelector("a.xx"))
1672
- * DOMUtils.hide(document.querySelectorAll("a.xx"))
1673
- * DOMUtils.hide("a.xx")
1674
- */
1675
- hide(target: DOMUtilsTargetElementType, checkVisiblie: boolean = true) {
1676
- const DOMUtilsContext = this;
1677
- if (target == null) {
1678
- return;
1679
- }
1680
- if (typeof target === "string") {
1681
- target = DOMUtilsContext.selectorAll(target);
1682
- }
1683
- if (target instanceof NodeList || target instanceof Array) {
1684
- target = target as HTMLElement[];
1685
- for (const element of target) {
1686
- DOMUtilsContext.hide(element, checkVisiblie);
1687
- }
1688
- } else {
1689
- target = target as HTMLElement;
1690
- target.style.display = "none";
1691
- if (checkVisiblie) {
1692
- if (DOMUtilsCommonUtils.isShow(target)) {
1693
- /* 仍然是显示,尝试使用强覆盖 */
1694
- target.style.setProperty("display", "none", "important");
1695
- }
1696
- }
1697
- }
1698
- }
1699
- /**
1700
- * 淡入元素
1701
- * @param element 当前元素
1702
- * @param duration 动画持续时间(毫秒),默认400毫秒
1703
- * @param callback 动画结束的回调
1704
- * @example
1705
- * // 元素a.xx淡入
1706
- * DOMUtils.fadeIn(document.querySelector("a.xx"),2500,()=>{
1707
- * console.log("淡入完毕");
1708
- * })
1709
- * DOMUtils.fadeIn("a.xx",undefined,()=>{
1710
- * console.log("淡入完毕");
1711
- * })
1712
- */
1713
- fadeIn(element: DOMUtilsTargetElementType, duration: number = 400, callback?: () => void) {
1714
- if (element == null) {
1715
- return;
1716
- }
1717
- const DOMUtilsContext = this;
1718
- if (typeof element === "string") {
1719
- element = DOMUtilsContext.selectorAll(element);
1720
- }
1721
- if (DOMUtilsCommonUtils.isNodeList(element)) {
1722
- // 设置
1723
- element.forEach(($ele) => {
1724
- DOMUtilsContext.fadeIn($ele as HTMLElement, duration, callback);
1725
- });
1726
- return;
1727
- }
1728
- element.style.opacity = "0";
1729
- element.style.display = "";
1730
- let start: number = null as any;
1731
- let timer: number = null as any;
1732
- function step(timestamp: number) {
1733
- if (!start) start = timestamp;
1734
- const progress = timestamp - start;
1735
- element = element as HTMLElement;
1736
- element.style.opacity = Math.min(progress / duration, 1).toString();
1737
- if (progress < duration) {
1738
- DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
1739
- } else {
1740
- if (callback && typeof callback === "function") {
1741
- callback();
1742
- }
1743
- DOMUtilsContext.windowApi.window.cancelAnimationFrame(timer);
1744
- }
1745
- }
1746
- timer = DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
1747
- }
1748
- /**
1749
- * 淡出元素
1750
- * @param element 当前元素
1751
- * @param duration 动画持续时间(毫秒),默认400毫秒
1752
- * @param callback 动画结束的回调
1753
- * @example
1754
- * // 元素a.xx淡出
1755
- * DOMUtils.fadeOut(document.querySelector("a.xx"),2500,()=>{
1756
- * console.log("淡出完毕");
1757
- * })
1758
- * DOMUtils.fadeOut("a.xx",undefined,()=>{
1759
- * console.log("淡出完毕");
1760
- * })
1761
- */
1762
- fadeOut(element: DOMUtilsTargetElementType, duration: number = 400, callback?: () => void) {
1763
- const DOMUtilsContext = this;
1764
- if (element == null) {
1765
- return;
1766
- }
1767
- if (typeof element === "string") {
1768
- element = DOMUtilsContext.selectorAll(element);
1769
- }
1770
- if (DOMUtilsCommonUtils.isNodeList(element)) {
1771
- // 设置
1772
- element.forEach(($ele) => {
1773
- DOMUtilsContext.fadeOut($ele as HTMLElement, duration, callback);
1774
- });
1775
- return;
1776
- }
1777
- element.style.opacity = "1";
1778
- let start: number = null as any;
1779
- let timer: number = null as any;
1780
- function step(timestamp: number) {
1781
- if (!start) start = timestamp;
1782
- const progress = timestamp - start;
1783
- element = element as HTMLElement;
1784
- element.style.opacity = Math.max(1 - progress / duration, 0).toString();
1785
- if (progress < duration) {
1786
- DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
1787
- } else {
1788
- element.style.display = "none";
1789
- if (typeof callback === "function") {
1790
- callback();
1791
- }
1792
- DOMUtilsContext.windowApi.window.cancelAnimationFrame(timer);
1793
- }
1794
- }
1795
- timer = DOMUtilsContext.windowApi.window.requestAnimationFrame(step);
1796
- }
1797
- /**
1798
- * 切换元素的显示和隐藏状态
1799
- * @param element 当前元素
1800
- * @param checkVisiblie 是否检测元素是否显示
1801
- * @example
1802
- * // 如果元素a.xx当前是隐藏,则显示,如果是显示,则隐藏
1803
- * DOMUtils.toggle(document.querySelector("a.xx"))
1804
- * DOMUtils.toggle("a.xx")
1805
- */
1806
- toggle(element: DOMUtilsTargetElementType, checkVisiblie?: boolean) {
1807
- const DOMUtilsContext = this;
1808
- if (typeof element === "string") {
1809
- element = DOMUtilsContext.selectorAll(element);
1810
- }
1811
- if (element == null) {
1812
- return;
1813
- }
1814
- if (DOMUtilsCommonUtils.isNodeList(element)) {
1815
- // 设置
1816
- element.forEach(($ele) => {
1817
- DOMUtilsContext.toggle($ele as HTMLElement);
1818
- });
1819
- return;
1820
- }
1821
- if (DOMUtilsContext.windowApi.globalThis.getComputedStyle(element).getPropertyValue("display") === "none") {
1822
- DOMUtilsContext.show(element, checkVisiblie);
1823
- } else {
1824
- DOMUtilsContext.hide(element, checkVisiblie);
1825
- }
1826
- }
1827
1552
  /**
1828
1553
  * 创建一个新的DOMUtils实例
1829
1554
  * @param option
@@ -1845,7 +1570,7 @@ class DOMUtils extends DOMUtilsEvent {
1845
1570
  selectionStart?: number | string,
1846
1571
  selectionEnd?: number | string
1847
1572
  ): DOMRect {
1848
- const DOMUtilsContext = this;
1573
+ const that = this;
1849
1574
  // Basic parameter validation
1850
1575
  if (!$input || !("value" in $input)) return $input;
1851
1576
  if (selectionStart == null) {
@@ -1914,7 +1639,7 @@ class DOMUtils extends DOMUtilsEvent {
1914
1639
  // 不能为空,不然获取不到高度
1915
1640
  const text = $input.value || "G",
1916
1641
  textLen = text.length,
1917
- fakeClone = DOMUtilsContext.windowApi.document.createElement("div");
1642
+ fakeClone = that.windowApi.document.createElement("div");
1918
1643
  if (selectionStart > 0) appendPart(0, selectionStart);
1919
1644
  const fakeRange = appendPart(selectionStart, selectionEnd);
1920
1645
  if (textLen > selectionEnd) appendPart(selectionEnd, textLen);
@@ -1928,7 +1653,7 @@ class DOMUtils extends DOMUtilsEvent {
1928
1653
  fakeClone.style.left = leftPos + "px";
1929
1654
  fakeClone.style.width = width + "px";
1930
1655
  fakeClone.style.height = height + "px";
1931
- DOMUtilsContext.windowApi.document.body.appendChild(fakeClone);
1656
+ that.windowApi.document.body.appendChild(fakeClone);
1932
1657
  const returnValue = fakeRange.getBoundingClientRect(); //Get rect
1933
1658
 
1934
1659
  fakeClone?.parentNode?.removeChild(fakeClone); //Remove temp
@@ -1942,7 +1667,7 @@ class DOMUtils extends DOMUtilsEvent {
1942
1667
  * @returns
1943
1668
  */
1944
1669
  function appendPart(start: number, end: number) {
1945
- const span = DOMUtilsContext.windowApi.document.createElement("span");
1670
+ const span = that.windowApi.document.createElement("span");
1946
1671
  span.style.cssText = cssDefaultStyles; //Force styles to prevent unexpected results
1947
1672
  span.textContent = text.substring(start, end);
1948
1673
  fakeClone.appendChild(span);
@@ -1950,10 +1675,10 @@ class DOMUtils extends DOMUtilsEvent {
1950
1675
  }
1951
1676
  // Computing offset position
1952
1677
  function getInputOffset() {
1953
- const body = DOMUtilsContext.windowApi.document.body,
1954
- win = DOMUtilsContext.windowApi.document.defaultView!,
1955
- docElem = DOMUtilsContext.windowApi.document.documentElement,
1956
- $box = DOMUtilsContext.windowApi.document.createElement("div");
1678
+ const body = that.windowApi.document.body,
1679
+ win = that.windowApi.document.defaultView!,
1680
+ docElem = that.windowApi.document.documentElement,
1681
+ $box = that.windowApi.document.createElement("div");
1957
1682
  $box.style.paddingLeft = $box.style.width = "1px";
1958
1683
  body.appendChild($box);
1959
1684
  const isBoxModel = $box.offsetWidth == 2;
@@ -1975,7 +1700,7 @@ class DOMUtils extends DOMUtilsEvent {
1975
1700
  * @returns
1976
1701
  */
1977
1702
  function getInputCSS<T extends boolean>(prop: string, isNumber: T): T extends true ? number : string {
1978
- const val = DOMUtilsContext.windowApi.document.defaultView!.getComputedStyle($input, null).getPropertyValue(prop);
1703
+ const val = that.windowApi.document.defaultView!.getComputedStyle($input, null).getPropertyValue(prop);
1979
1704
  if (isNumber) {
1980
1705
  return parseFloat(val) as T extends true ? number : string;
1981
1706
  } else {
@@ -1983,13 +1708,209 @@ class DOMUtils extends DOMUtilsEvent {
1983
1708
  }
1984
1709
  }
1985
1710
  }
1986
- /** 获取 animationend 在各个浏览器的兼容名 */
1987
- getAnimationEndNameList() {
1988
- return DOMUtilsCommonUtils.getAnimationEndNameList();
1711
+ /**
1712
+ * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
1713
+ * @param cssText css字符串
1714
+ * @returns 返回添加的CSS标签
1715
+ * @example
1716
+ * DOMUtils.addStyle("html{}");
1717
+ * > <style type="text/css">html{}</style>
1718
+ */
1719
+ addStyle(cssText: string): HTMLStyleElement;
1720
+ addStyle(cssText: string): HTMLStyleElement {
1721
+ if (typeof cssText !== "string") {
1722
+ throw new Error("DOMUtils.addStyle 参数cssText 必须为String类型");
1723
+ }
1724
+ const $css = this.createElement("style", {
1725
+ type: "text/css",
1726
+ innerHTML: cssText,
1727
+ });
1728
+ if (this.windowApi.document.head) {
1729
+ /* 插入head最后 */
1730
+ this.windowApi.document.head.appendChild($css);
1731
+ } else if (this.windowApi.document.documentElement.childNodes.length === 0) {
1732
+ /* 插入#html后 */
1733
+ this.windowApi.document.documentElement.appendChild($css);
1734
+ } else {
1735
+ /* 插入#html第一个元素前 */
1736
+ this.windowApi.document.documentElement.insertBefore($css, this.windowApi.document.documentElement.childNodes[0]);
1737
+ }
1738
+ return $css;
1739
+ }
1740
+ /**
1741
+ * 检测点击的地方是否在该元素区域内
1742
+ * @param $el 需要检测的元素
1743
+ * @returns
1744
+ * + true 点击在元素上
1745
+ * + false 未点击在元素上
1746
+ * @example
1747
+ * DOMUtils.checkUserClickInNode(document.querySelector(".xxx"));
1748
+ * > false
1749
+ **/
1750
+ checkUserClickInNode($el: Element | Node | HTMLElement) {
1751
+ const that = this;
1752
+ if (!CommonUtils.isDOM($el)) {
1753
+ throw new Error("Utils.checkUserClickInNode 参数 targetNode 必须为 Element|Node 类型");
1754
+ }
1755
+ const clickEvent = that.windowApi.window.event as PointerEvent;
1756
+ const touchEvent = that.windowApi.window.event as TouchEvent;
1757
+ const $click = clickEvent?.composedPath()?.[0] as HTMLElement;
1758
+
1759
+ // 点击的x坐标
1760
+ const clickPosX = clickEvent?.clientX != null ? clickEvent.clientX : touchEvent.touches[0].clientX;
1761
+ // 点击的y坐标
1762
+ const clickPosY = clickEvent?.clientY != null ? clickEvent.clientY : touchEvent.touches[0].clientY;
1763
+ const {
1764
+ /* 要检测的元素的相对屏幕的横坐标最左边 */
1765
+ left: elementPosXLeft,
1766
+ /* 要检测的元素的相对屏幕的横坐标最右边 */
1767
+ right: elementPosXRight,
1768
+ /* 要检测的元素的相对屏幕的纵坐标最上边 */
1769
+ top: elementPosYTop,
1770
+ /* 要检测的元素的相对屏幕的纵坐标最下边 */
1771
+ bottom: elementPosYBottom,
1772
+ } = (<HTMLElement>$el).getBoundingClientRect();
1773
+ if (
1774
+ clickPosX >= elementPosXLeft &&
1775
+ clickPosX <= elementPosXRight &&
1776
+ clickPosY >= elementPosYTop &&
1777
+ clickPosY <= elementPosYBottom
1778
+ ) {
1779
+ return true;
1780
+ } else if (($click && $el.contains($click)) || $click == $el) {
1781
+ /* 这种情况是应对在界面中隐藏的元素,getBoundingClientRect获取的都是0 */
1782
+ return true;
1783
+ } else {
1784
+ return false;
1785
+ }
1786
+ }
1787
+ /**
1788
+ * 删除某个父元素,父元素可能在上层或上上层或上上上层...
1789
+ * @param $el 当前元素
1790
+ * @param parentSelector 判断是否满足父元素,参数为当前处理的父元素,满足返回true,否则false
1791
+ * @returns
1792
+ * + true 已删除
1793
+ * + false 未删除
1794
+ * @example
1795
+ * DOMUtils.deleteParentNode(document.querySelector("a"),".xxx");
1796
+ * > true
1797
+ **/
1798
+ deleteParentNode($el: Node | HTMLElement | Element | null, parentSelector: string): boolean;
1799
+ deleteParentNode($el: Node | HTMLElement | Element | null, parentSelector: string) {
1800
+ if ($el == null) {
1801
+ return;
1802
+ }
1803
+ if (!CommonUtils.isDOM($el)) {
1804
+ throw new Error("DOMUtils.deleteParentNode 参数 target 必须为 Node|HTMLElement 类型");
1805
+ }
1806
+ if (typeof parentSelector !== "string") {
1807
+ throw new Error("DOMUtils.deleteParentNode 参数 targetSelector 必须为 string 类型");
1808
+ }
1809
+ let result = false;
1810
+ const $parent = domUtils.closest($el as HTMLElement, parentSelector);
1811
+ if ($parent) {
1812
+ this.remove($parent);
1813
+ result = true;
1814
+ }
1815
+ return result;
1989
1816
  }
1990
- /** 获取 transitionend 在各个浏览器的兼容名 */
1991
- getTransitionEndNameList() {
1992
- return DOMUtilsCommonUtils.getTransitionEndNameList();
1817
+ /**
1818
+ * 定位元素上的字符串,返回一个迭代器
1819
+ * @param $el 目标元素
1820
+ * @param text 需要定位的字符串
1821
+ * @param filter (可选)过滤器函数,返回值为true是排除该元素
1822
+ * @example
1823
+ * let textIterator = DOMUtils.findElementsWithText(document.documentElement,"xxxx");
1824
+ * textIterator.next();
1825
+ * > {value: ?HTMLElement, done: boolean, next: Function}
1826
+ */
1827
+ findElementsWithText<T extends HTMLElement | Element | Node>(
1828
+ $el: T,
1829
+ text: string,
1830
+ filter?: (element: T) => boolean
1831
+ ): Generator<HTMLElement | ChildNode, void, any>;
1832
+ *findElementsWithText<T extends HTMLElement | Element | Node>(
1833
+ $el: T,
1834
+ text: string,
1835
+ filter?: (element: T) => boolean
1836
+ ) {
1837
+ const that = this;
1838
+ if ((<HTMLElement>$el).outerHTML.includes(text)) {
1839
+ if ((<HTMLElement>$el).children.length === 0) {
1840
+ const filterResult = typeof filter === "function" ? filter($el) : false;
1841
+ if (!filterResult) {
1842
+ yield $el as any;
1843
+ }
1844
+ } else {
1845
+ const $text = Array.from($el.childNodes).filter(($child) => $child.nodeType === Node.TEXT_NODE);
1846
+ for (const $child of $text) {
1847
+ if ((<HTMLElement>$child).textContent.includes(text)) {
1848
+ const filterResult = typeof filter === "function" ? filter($el) : false;
1849
+ if (!filterResult) {
1850
+ yield $child;
1851
+ }
1852
+ }
1853
+ }
1854
+ }
1855
+ }
1856
+
1857
+ for (let index = 0; index < (<HTMLElement>$el).children.length; index++) {
1858
+ const $child = (<HTMLElement>$el).children[index] as T;
1859
+ yield* that.findElementsWithText($child, text, filter);
1860
+ }
1861
+ }
1862
+ /**
1863
+ * 寻找可见元素,如果元素不可见,则向上找它的父元素直至找到,如果父元素不存在则返回null
1864
+ * @param $el
1865
+ * @example
1866
+ * let visibleElement = DOMUtils.findVisibleElement(document.querySelector("a.xx"));
1867
+ * > <HTMLElement>
1868
+ */
1869
+ findVisibleElement($el: HTMLElement | Element | Node) {
1870
+ let $current = $el as HTMLElement;
1871
+ while ($current) {
1872
+ const rect = $current.getBoundingClientRect();
1873
+ if ((rect as any).length) {
1874
+ return $current;
1875
+ }
1876
+ $current = $current.parentElement as HTMLElement;
1877
+ }
1878
+ return null;
1879
+ }
1880
+ /**
1881
+ * 将元素上的文本或元素使用光标进行选中
1882
+ *
1883
+ * 注意,如果设置startIndex和endIndex,且元素上并无可选则的坐标,那么会报错
1884
+ * @param $el 目标元素
1885
+ * @param childTextNode 目标元素下的#text元素
1886
+ * @param startIndex (可选)开始坐标,可为空
1887
+ * @param endIndex (可选)结束坐标,可为空
1888
+ * @example
1889
+ * DOMUtils.setElementSelection(document.querySelector("span"));
1890
+ */
1891
+ setElementSelection(
1892
+ $el: HTMLElement | Element | Node,
1893
+ childTextNode?: ChildNode,
1894
+ startIndex?: number,
1895
+ endIndex?: number
1896
+ ): void {
1897
+ const range = this.windowApi.document.createRange();
1898
+ range.selectNodeContents($el);
1899
+ if (childTextNode) {
1900
+ if (childTextNode.nodeType !== Node.TEXT_NODE) {
1901
+ throw new TypeError("childTextNode必须是#text元素");
1902
+ }
1903
+ if (startIndex != null && endIndex != null) {
1904
+ range.setStart(childTextNode, startIndex);
1905
+ range.setEnd(childTextNode, endIndex);
1906
+ }
1907
+ }
1908
+
1909
+ const selection = this.windowApi.globalThis.getSelection();
1910
+ if (selection) {
1911
+ selection.removeAllRanges();
1912
+ selection.addRange(range);
1913
+ }
1993
1914
  }
1994
1915
  }
1995
1916